MATLAB Troubleshooting Syntax Errors in MATLAB 'if' Blocks

  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab
AI Thread Summary
The discussion centers on a coding issue in an 'if' block in MATLAB, where the statement is intended to check if any of four scalars are equal to zero. The error encountered is due to incorrect parentheses around the third scalar, which leads to the message that operands must be convertible to logical scalar values. The correct syntax should ensure that the comparison is made directly, as in (scalar3 == 0), rather than (scalar3) == 0. This oversight caused confusion, but once identified, it clarified the problem and resolved the error.
hoffmann
Messages
65
Reaction score
0
What is wrong with this statement in an 'if' block?

if ((scalar1 == 0) || (scalar2 == 0) || (scalar3) == 0 || (scalar4 == 0))

I basically want to say: if any of the four scalars are individually equal to zero, then execute the subsequent statements.

I receive the following error in the command line:
Operands to the || and && operators must be convertible to logical scalar values.

Any suggestions?
 
Physics news on Phys.org
hoffmann said:
What is wrong with this statement in an 'if' block?

if ((scalar1 == 0) || (scalar2 == 0) || (scalar3) == 0 || (scalar4 == 0))

I basically want to say: if any of the four scalars are individually equal to zero, then execute the subsequent statements.

I receive the following error in the command line:
Operands to the || and && operators must be convertible to logical scalar values.

Any suggestions?
First, thanks for an exceedingly rare Matlab question that cannot be answered "rtfm" :smile:

It looks to me like the problem is the parentheses around scalar3

(scalar3)==0

instead of

(scalar3==0)
 
You know, I thought I had scoured the whole line for a possible mistake. I swore that there wasn't one...but thanks for pointing that out. Wow, I feel like a fool, but at least I can laugh about it now.
 
DaleSpam said:
First, thanks for an exceedingly rare Matlab question that cannot be answered "rtfm" :smile:

It looks to me like the problem is the parentheses around scalar3

(scalar3)==0

instead of

(scalar3==0)

Eyes like a hawk.:smile:

I spent a good ten minutes looking at the question while trying to figure out why the code wouldn't work. Then I noticed the brackets!
 
Back
Top