0

I have the following:

d=[1 2 3 4 5 6 7]

I want Matlab to assign a day name to every number by doing a loop or

any suitable method as follows:

1 =tuesday

2=wednesday

.

.

.

7=monday

the results I am aiming to get after running the program is :

the Matlab window asks the user to enter a number from 1 to 7

n=('enter a number from 1 to 7')

then,

if we enter ,for example, 4 , this means that the printed result is: Friday

or

if we entered , for example , 7, this means that the printed result is: Monday

and so on

Is there any way to do this

regards

zellus
  • 9,617
  • 5
  • 39
  • 56
g_kfupm
  • 11
  • 1
  • 1

2 Answers2

5

You could use a cell array, which allows you to store an array of text strings. The curly bracket is the key:

>> weekdays = {'Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'};
>> weekdays{4}

ans =

Thurs

Edit: You can get the relevant number from the user by using MATLAB's input function:

n = input('Enter your number:');
disp(weekdays{n})
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104
  • 1
    Good approach as long as you don't want to change (add, remove) elements. Which will be a rare case for weekdays. – zellus Nov 01 '10 at 21:40
1

Using a map might be one approach:

weekDays = containers.Map({1, 2, 3, 4, 5, 6, 7} , ...
{'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'});

number = input('enter a number from 1 to 7');
disp(sprintf('You did choose %s\n', weekDays(number)));


EDIT: Using the solution by Bill Cheatham you end up with

weekdays = {'Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun'};
number = input('enter a number from 1 to 7');
disp(sprintf('You did choose %s\n', weekdays{number}));
zellus
  • 9,617
  • 5
  • 39
  • 56
  • thanks. but what is this function: containers.Map . I am using MATLAB. I think it is not defined there.regards – g_kfupm Nov 01 '10 at 21:31
  • @g_kfupm: What version of MATLAB are you using. – zellus Nov 01 '10 at 21:32
  • @g_kfupm: it's Java, but there is a Java interpreter in MATLAB, and it works as advertised. – André Caron Nov 01 '10 at 21:33
  • @André Caron: Sorry for disagreeing, but containers.Map is part of the MATLAB commands. As MATLAB allows Java scripting, you might use a Java hashmap as well. – zellus Nov 01 '10 at 21:35
  • Containers.Map was added in MATLAB 7.7 (R2008b) as mentioned in http://stackoverflow.com/questions/3591942/hash-tables-in-matlab/3592050#3592050 – zellus Nov 01 '10 at 21:37
  • @g_kfupm: Then http://stackoverflow.com/questions/4073060/how-can-we-assign-letters-to-numbers/4073267#4073267 by Bill Cheatham might be an option, or switching to python :-). – zellus Nov 01 '10 at 21:50
  • zellus: but what Bill Cheatham has done is by simply looking to the matrix and then extracting. Can we get it programmed automatically and show the result by simply pressing a number from 1 to 7. regards. thanks for everyone helps in this question. – g_kfupm Nov 01 '10 at 22:04
  • @g_kfupm: I tried to give a 'full' solution in the edited section of this post, taking Bill Cheathams approach. – zellus Nov 01 '10 at 22:11
  • zellus: perfect. It works perfectly. thanks a lot , I appreciate your cooperation. Thanks to Bill Cheatham. Thanks to everyone participates to solve my problem. regards – g_kfupm Nov 01 '10 at 22:16
  • @g_kfupm: Your welcome, just added a disp() in order to show the result. – zellus Nov 01 '10 at 22:18