MATLAB Answers

0

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

Asked by Crystal Lee on 13 Aug 2018 at 0:15
Latest activity Commented on by Crystal Lee on 13 Aug 2018 at 9:51

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 Comments

Must be more than that; that code runs w/o any error here...

>> A=3;
>> if A==[1, 3, 3],disp('ok'),end
>> A=[1 3 3];
>> if A==[1, 3, 3],disp('ok'),end
ok
>> 

Show us the context including the error message with all the text generated...

@Crystal Lee: please show us the complete error message. This means all of the red text.

There is no red text, I was just wondering if I could get rid of the orange warning.

Sign in to comment.

Tags

Products

2 Answers

Answer by Stephen Cobeldick on 13 Aug 2018 at 5:16
Edited by Stephen Cobeldick on 13 Aug 2018 at 5:19
 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.

  1 Comment

Sign in to comment.


Answer by KSSV
on 13 Aug 2018 at 4:48

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 Comments

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.

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

Sorry, I confused warning with error.

Sign in to comment.