Troubleshooting Syntax Errors in MATLAB 'if' Blocks

  • Context: MATLAB 
  • Thread starter Thread starter hoffmann
  • Start date Start date
  • Tags Tags
    Matlab
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
3 replies · 15K views
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!