MATLAB Answers


'Or' statement in matlab?

Asked by Aadil
on 7 Sep 2012
Latest activity Edited by Simran Wasu on 20 Feb 2016

How can I do this without using else if?:

if balls == 0 & ( x > 5 OR y > 7)
    run xscript



Show 1 older comment

what I meant is that I can use else if to do the above, but how can I avoid having to write the commands twice like this:

if balls == 0 & x > 5
     run xscript
    elseif balls == 0 & y > 7
         run xscript

Image Analyst understood faster and gave you the answer...

use '||' for OR command (without quotes).

Log in to comment.

1 Answer

Answer by Image Analyst
on 7 Sep 2012
Edited by Image Analyst
on 7 Sep 2012
 Accepted Answer

You need two &&:

if balls == 0 && ( x > 5 || y > 7)
    % run xscript


@Aadil: Simply ask Google for "Matlab &&" and you weill immediate get:, which refers to Or type "help or".

I've seen that before - where the vertical lines don't appear. However I can't reproduce anymore. For me | appears both in regular text, and code formatted text. By the way, I edited by answer to change "run xscript" to be just "xscript" since the "run" command like you had is not needed.

ok thanks everyone

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today