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 CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by