Asked by Crystal Lee
on 13 Aug 2018

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?

Answer by Stephen Cobeldick
on 13 Aug 2018

Edited by Stephen Cobeldick
on 13 Aug 2018

Accepted Answer

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.

Answer by KSSV
on 13 Aug 2018

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.

Stephen Cobeldick
on 13 Aug 2018

Aha, thank you KSSV. This is a new warning (not an error, as the question states), that must have been introduced with some new MATLAB version. May I ask what version you are using?

Note that this issue is not because "With this operator ==, MATLAB expects a scalar value", but is because if expects a scalar value: if expects the scalar, not ==.

Also "YOu need to use the operator == with a scalar value..." is not correct: a simple all, any, etc would solve this quite easily, as my answer shows, by providing the scalar that if expects.

KSSV
on 13 Aug 2018

Yes....it is not a error..it is a warning....I am using 2015b.

Crystal Lee
on 13 Aug 2018

Sorry, I confused warning with error.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/414593-how-to-get-rid-of-warning-unexpected-use-of-in-a-scalar-context#comment_599021

## Stephen Cobeldick (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/414593-how-to-get-rid-of-warning-unexpected-use-of-in-a-scalar-context#comment_599033

## Crystal Lee (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/414593-how-to-get-rid-of-warning-unexpected-use-of-in-a-scalar-context#comment_599119

Sign in to comment.