how to combine switch statements for final message?
13 views (last 30 days)
Show older comments
I was stuck on combining 2 switch statements for a final message of a program.
here is my code:
vec= input('enter provided code','s')-'0'
day=(vec(1)*vec(2)-vec(3))
switch day
case 1
disp ('Monday')
case 2
disp('Tuesday')
case 3
disp('Wednesday')
case 4
disp('Thursday')
....
if (mod(vec(4),3))
rvp= (vec(6)-vec(5))
switch rvp
case 1
disp('B')
case 2
disp('L')
case 3
disp('RC')
....
X= ['rescue at', str2num(day), 'at', str2num(rvp)]
disp(X)
here, i want it to display: rescue at Monday at L or something of that sort but when i try to enter something in i get:
enter provided code510129
Friday
rescue at5at1
can you please help how to put switch statements part of the final message using the display function?
thanks
Answers (1)
Walter Roberson
on 19 Oct 2015
Do not disp() the values as soon as they are calculated. Assign them to a variable instead. And then at the end, put all the variables together.
2 Comments
Walter Roberson
on 20 Oct 2015
Edited: Walter Roberson
on 20 Oct 2015
switch day
case 1
dayname = 'Monday';
case 2
dayname = 'Tuesday';
otherwise
dayname = '?Day?';
end
switch rvp
case 1
rvpcode = 'B';
case 2
rvpcode = 'L';
otherwise
rvpcode = '?RVP?';
end
result = [dayname, rvpcode];
Or....
daynames = {'Monday', 'Tuesday', 'Wednesday' ...};
rvpcodes = {'B', 'L', 'RC' ...};
dayname = daynames{day};
rvpcode = rvpcodes{rvp};
result = [dayname, rvpcode];
Notice the complete lack of switch/case
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!