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

41 次查看(过去 30 天)
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 个评论
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 个评论

请先登录,再进行评论。

类别

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