Multple switch expressions needed to run

1 回表示 (過去 30 日間)
Tim Decuypere
Tim Decuypere 2017 年 11 月 24 日
コメント済み: Tim Decuypere 2017 年 11 月 25 日
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 件のコメント
Rik
Rik 2017 年 11 月 24 日
You could put the switch block in its own function and use cellfun or even a for-loop.
Tim Decuypere
Tim Decuypere 2017 年 11 月 25 日
Great suggestion, thanks!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2017 年 11 月 24 日
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

その他の回答 (1 件)

KVM
KVM 2017 年 11 月 24 日
編集済み: Walter Roberson 2017 年 11 月 24 日
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

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by