Multple switch expressions needed to run
조회 수: 1 (최근 30일)
이전 댓글 표시
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
2017년 11월 24일
You could put the switch block in its own function and use cellfun or even a for-loop.
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
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
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!