75 views (last 30 days)

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?

Stephen Cobeldick
on 13 Aug 2018

Edited: Stephen Cobeldick
on 13 Aug 2018

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.

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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

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

⋮## Direct link to this comment

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

## Direct link to this comment

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

⋮## Direct link to this comment

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

## Direct link to this comment

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

⋮## Direct link to this comment

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

Sign in to comment.