Multple switch expressions needed to run

1 Ansicht (letzte 30 Tage)
Tim Decuypere
Tim Decuypere am 24 Nov. 2017
Kommentiert: Tim Decuypere am 25 Nov. 2017
I need some help on executing multiple cases when using the "Switch" function. An example to clearify this
scenario = {'a', 'b'}
switch scenario
case 'a'
disp('Hello ')
case 'b'
disp('World')
case 'c'
disp('dont display')
end
The output what i'm looking for word be:
'Hello '
'World'
The idea is that "Case" would check if the variable is in scenario and accordinly run it. Could anyone please give me a suggestion how I could do this elegantly? It seems this only works the other way around.
Thanks a lot!
  2 Kommentare
Rik
Rik am 24 Nov. 2017
You could put the switch block in its own function and use cellfun or even a for-loop.
Tim Decuypere
Tim Decuypere am 25 Nov. 2017
Great suggestion, thanks!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Nov. 2017
As Rik Wisselink hinted:
scenario = {'a','b'};
for k = 1:numel(scenario)
switch scenario{k}
case 'a'
disp('Hello ')
case 'b'
disp('World')
case 'c'
disp('dont display')
end
end
displays:
Hello
World

Weitere Antworten (1)

KVM
KVM am 24 Nov. 2017
Bearbeitet: Walter Roberson am 24 Nov. 2017
scenario = {'a', 'b'}
for i=1:length(scenario);
switch scenario{i}
case 'a'
disp('Hello ')
case 'b'
disp('World')
case 'c'
disp('dont display')
end
end

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by