필터 지우기
필터 지우기

Can I use case 1 || case 2 while using switch thread?

조회 수: 125 (최근 30일)
Akhil  Reddy
Akhil Reddy 2016년 6월 7일
댓글: Swapnil Devikar 2020년 3월 27일
switch matlab_review
case 'good' || case 'interesting'
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end
  댓글 수: 2
Stephen23
Stephen23 2016년 6월 7일
@Akhil Reddy: did you read the switch documentation? It explains exactly how to do this... and it has examples too!
Akhil  Reddy
Akhil Reddy 2016년 6월 22일
Thank you :)

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2016년 6월 7일
As per the documentation of switch, case, otherwise, to include several possible match for a case, group them in a cell array.
switch matlab_review
case {'good', 'interesting'}
fprintf('Review is positive\n');
otherwise
fprintf('Not so good review\n');
end
It's always a good idea to read the documentation rather than trying to make up your own syntax.
  댓글 수: 1
Swapnil Devikar
Swapnil Devikar 2020년 3월 27일
Just an observation here. The documentation talks about combining test cases as you have correctly pointed out. But it doesn't talk about combining a case and a "default" case. For instance, in your example, it is not cleat if it is acceptable to combine {'good', 'otherwise'}.
It matters because "otherwise" is a special case.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by