필터 지우기
필터 지우기

How to get rid of Warning"Unexpected use of '[' in a scalar context."

조회 수: 56 (최근 30일)
Crystal Lee
Crystal Lee 2018년 8월 13일
편집: Stephen23 2020년 9월 17일
a part of the function has:
if the input A (a vector) matches what is stated, the statement runs.
if A == [1,3,3]
% statement
end
and it gives me this error: 'Unexpected use of '[' in a scalar context. How can I fix this?
  댓글 수: 3
Stephen23
Stephen23 2018년 8월 13일
편집: Stephen23 2018년 8월 13일
@Crystal Lee: please show us the complete error message. This means all of the red text.
Crystal Lee
Crystal Lee 2018년 8월 13일
There is no red text, I was just wondering if I could get rid of the orange warning.

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

채택된 답변

Stephen23
Stephen23 2018년 8월 13일
편집: Stephen23 2018년 8월 13일
Use all or any, like this:
if all(A==[1,3,3])
Or right-click the pop-up warning box and follow the instructions to suppress it.
  댓글 수: 3
Thiago  Rodrigues
Thiago Rodrigues 2020년 9월 17일
What the explanation for the "all" fix the warning?
Stephen23
Stephen23 2020년 9월 17일
편집: Stephen23 2020년 9월 17일
"What the explanation for the "all" fix the warning?"
Given a vector input the output from all is scalar. A scalar condition is easier to understand, which is why it is recommended by that warning.

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

추가 답변 (1개)

KSSV
KSSV 2018년 8월 13일
When you use A == [1 3 3] ;
With this operator ==, MATLAB expects a scalar value, but the way used here, it gives array as output. So the warning pops out. The way you use, won't work out. YOu need to use the operator == with a scalar value, so the if condition works fine.
  댓글 수: 5
KSSV
KSSV 2018년 8월 13일
Yes....it is not a error..it is a warning....I am using 2015b.
Crystal Lee
Crystal Lee 2018년 8월 13일
Sorry, I confused warning with error.

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

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by