필터 지우기
필터 지우기

Using switch case with multiple variables (instead of just n, with n1, n2, etc.).

조회 수: 162 (최근 30일)
Hello all,
I'm trying to find a way to run the following code:
x1=4;
x2=4;
y1=4;
y2=4;
switch x1,x2,y1,y2
case x1>0 && y1<0 && ((y2>0) || (y2<0 && x2<0) || (x2>0 && y2<y1))
disp('Subtract from 360')
otherwise
disp('Keep as is')
end
and I get this error:
Error: File: untitled5.m Line: 5 Column: 11
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or
other syntax error. To construct matrices, use brackets instead of parentheses.
I'm not sure if I can use switch case with mutliple variables like that. I'd appreciate it if anyone could lend me hand about this.

채택된 답변

Cris LaPierre
Cris LaPierre 2019년 3월 10일
You can only switch on one variable. Try this instead
x1=4;
x2=4;
y1=-4;
y2=4;
switch true
case x1>0 && y1<0 && (y2>0 || y2<0 && x2<0 || x2>0 && y2<y1)
disp('Subtract from 360')
otherwise
disp('Keep as is')
end
  댓글 수: 2
Arian Kolahi Sohrabi
Arian Kolahi Sohrabi 2019년 3월 10일
편집: Arian Kolahi Sohrabi 2019년 3월 10일
Wow. Thank you so much.
Quick question: how did you know "true" could be used there like that?
Steven Lord
Steven Lord 2019년 3월 10일
Or as an even easier approach:
x1=4;
x2=4;
y1=-4;
y2=4;
if x1>0 && y1<0 && (y2>0 || y2<0 && x2<0 || x2>0 && y2<y1)
disp('Subtract from 360')
else
disp('Keep as is')
end
If you need to add more conditions, add elseif blocks inside the if block, before the else keyword.
Alternately, since this looks like you're using x and y coordinates to generate an angle, the atan2d function may be of use to you.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by