how to combine switch statements for final message?

20 visualizaciones (últimos 30 días)
Astha Sharma
Astha Sharma el 19 de Oct. de 2015
Editada: Walter Roberson el 20 de Oct. de 2015
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

Respuestas (1)

Walter Roberson
Walter Roberson el 19 de Oct. de 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 comentarios
Astha Sharma
Astha Sharma el 19 de Oct. de 2015
how would i do that with multiple cases? can you please give me an example?
Walter Roberson
Walter Roberson el 20 de Oct. de 2015
Editada: Walter Roberson el 20 de Oct. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by