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

46 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 13 Ago 2018
Modificato: Stephen23 il 13 Ago 2018
@Crystal Lee: please show us the complete error message. This means all of the red text.
Crystal Lee
Crystal Lee il 13 Ago 2018
There is no red text, I was just wondering if I could get rid of the orange warning.

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 13 Ago 2018
Modificato: Stephen23 il 13 Ago 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.
  3 Commenti
Stephen23
Stephen23 il 17 Set 2020
Modificato: Stephen23 il 17 Set 2020
"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.

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 13 Ago 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.
  5 Commenti
KSSV
KSSV il 13 Ago 2018
Yes....it is not a error..it is a warning....I am using 2015b.

Accedi per commentare.

Categorie

Scopri di più su Scope Variables and Generate Names in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by