Why can this expression be logically evaluated but not used in an if-statements?

조회 수: 2 (최근 30일)
Michel
Michel 2023년 1월 13일
편집: Fangjun Jiang 2023년 1월 13일
If would like to make an if-statements that depends on the following condition:
nargin > 9 && cell2mat(varargin) == 1
To my disappointment, for nargin = 10 and varargin = 0, the expression can be evaluated with no problems in debugging mode when I run my function (the result being a logical 0). However, if I want to make an if-clause that starts with it, it throws an error message in the same line:
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values.
Use the ANY or ALL functions to reduce operands to logical scalar values.
How can I deal with this and, more importantly, how can this even happen? If Matlab has a problem with my operands, how come it doesn't when I run it from the console?
  댓글 수: 2
Stephen23
Stephen23 2023년 1월 13일
편집: Stephen23 2023년 1월 13일
"Why can this expression be logically evaluated but not used in an if-statements?"
The error has nothing to do with IF, as the error message states it is due to the inputs you provide to &&.
"How can I deal with this"
Perhaps you could follow the advice given in the error message.
But first you need to define what should happen when you collect multiple arguments with VARARGIN:
  • must they all be empty except one?
  • must they all be scalar?
  • can they be non-scalar?
  • etc. etc
We cannot guess this for you. All we can see is that your code (apparently) concatenates multiple arrays (scalar, empty, we don't know because you did not say) into one array, which you then expect that array to be scalar. But it clearly isn't.
"how can this even happen?"
Because the inputs that you concatenate together create a non-scalar array. Take a look at this:
cell2mat({[],1,[],2}) % output is not scalar
ans = 1×2
1 2
Michel
Michel 2023년 1월 13일
Thank you for your reply. I'm sorry about my frustrated tone and the fact that I didn't elaborate any further.
In my case, varargin contains just a single value and if it is 1, something should happen. If no varargin input or any other number beside 1 is provided, something else should happen.
I just ran my code again and, with the exact same if-statement, somehow it didn't throw an error in any of the cases (1, any number or no number).

댓글을 달려면 로그인하십시오.

답변 (1개)

Fangjun Jiang
Fangjun Jiang 2023년 1월 13일
편집: Fangjun Jiang 2023년 1월 13일
"somehow it didn't throw an error in any of the cases (1, any number or no number)."
  • The coding problem still exists.
  • The error happens when varargin is empty (an empty cell), see case 4
  • The error didn't happen when varargin is empty, see case 3, is because the "false" ahead of "&&" operator. It is called short-circuiting. See "doc &&"
The better solution is to use isempty(varargin) to check if varargin is empty or not, first.
true && cell2mat({1})==1
ans = logical
1
true && cell2mat({0})==1
ans = logical
0
false && cell2mat({})==1
ans = logical
0
true && cell2mat({})==1
Operands to the logical AND (&&) and OR (||) operators must be convertible to logical scalar values. Use the ANY or ALL functions to reduce operands to logical scalar values.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by