Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
if statement in if clause

Subject: if statement in if clause

From: Yayp Rafique

Date: 9 Oct, 2010 22:19:03

Message: 1 of 7

Hi all,

I am a Java Developer and for the first time am doing the heavy lifting in MATLAB. I am writing a parser for a text file, hence some of the situations that arise might require measures that are not the most ideal but need to be done that way.

My very simple question is that how can I write an if statement in the if-clause of a prior if statement. this isn't like the "easy" if statement where I can write the nested if statement in then-clause or the else-clause. i think its better if i explain through an example what I would like:

if ( if (Z>0);A;else;B;end) > 0 && if (Z<0);C;else;D;end) > 0 )
      F=5
 else
      G=3
 end

although this is not the correct syntax in MATLAB this is what I would like. The solution of "solving them the nested if statements before and saving their results and then using these results" will not work because I am writing a parser and I won't know the depth of nesting before hand. I have to generate a MATLAB script as the output of my parser. I would really appreciate feedback on how to do this.

Subject: if statement in if clause

From: dpb

Date: 9 Oct, 2010 22:23:13

Message: 2 of 7

Yayp Rafique wrote:
...
> My very simple question is that how can I write an if statement in the
> if-clause of a prior if statement. this isn't like the "easy" if
> statement where I can write the nested if statement in then-clause or
> the else-clause. i think its better if i explain through an example what
> I would like:
>
> if(if(Z>0);A;else;B;end) > 0 && if(Z<0);C;else;D;end) > 0 )
     F=5
> else
       G=3
> end
>
> although this is not the correct syntax in MATLAB this is what I would
> like.

Matlab has no such syntax nor way I can think of to simulate it

> The solution of "solving them the nested if statements before and
> saving their results and then using these results" will not work because
> I am writing a parser and I won't know the depth of nesting before hand.
> I have to generate a MATLAB script as the output of my parser. I would
> really appreciate feedback on how to do this.

State machine comes to mind...

--

Subject: if statement in if clause

From: Yayp Rafique

Date: 9 Oct, 2010 23:34:03

Message: 3 of 7

Can u clarify a little of what you mean? I am relatively a novice in MATLAB so don't really know how to manipulate state machines. Isn't there a simple syntactical solution that can make the code run?


dpb <none@non.net> wrote in message <i8qq8f$nh7$1@news.eternal-september.org>...
> Yayp Rafique wrote:
> ...
> > My very simple question is that how can I write an if statement in the
> > if-clause of a prior if statement. this isn't like the "easy" if
> > statement where I can write the nested if statement in then-clause or
> > the else-clause. i think its better if i explain through an example what
> > I would like:
> >
> > if(if(Z>0);A;else;B;end) > 0 && if(Z<0);C;else;D;end) > 0 )
> F=5
> > else
> G=3
> > end
> >
> > although this is not the correct syntax in MATLAB this is what I would
> > like.
>
> Matlab has no such syntax nor way I can think of to simulate it
>
> > The solution of "solving them the nested if statements before and
> > saving their results and then using these results" will not work because
> > I am writing a parser and I won't know the depth of nesting before hand.
> > I have to generate a MATLAB script as the output of my parser. I would
> > really appreciate feedback on how to do this.
>
> State machine comes to mind...
>
> --

Subject: if statement in if clause

From: Nasser M. Abbasi

Date: 9 Oct, 2010 23:47:56

Message: 4 of 7

On 10/9/2010 4:34 PM, Yayp Rafique wrote:
> Can u clarify a little of what you mean? I am relatively a novice in MATLAB so don't
> really know how to manipulate state machines. Isn't there a simple
 > syntactical solution that can make the code run?
>

Why everyone seems to top post these days?

ref your question:

I think he said state machine in the programming sense. It is where you
write down the states that the program can be in at any one time. Then
you have a specific action to do when in such state, and then transfer
the state of the program to another state.

it is like a directed graph. nodes are states. When in a node, you
perform action, then jump to another state. and so on.

Implement as a large case or switch statement. Initialize the program to
one initial state. There is one end state to terminate.

It just makes complicated logic simpler to program than having to do if
then else many times and deeply nested.


It has nothing to do with 'matlab'.

--Nasser

Subject: if statement in if clause

From: Image Analyst

Date: 10 Oct, 2010 00:34:04

Message: 5 of 7

"Yayp Rafique" <ysheikh1984@hotmail.com> wrote in message <i8qpon$65s$1@fred.mathworks.com>...

> if ( if (Z>0);A;else;B;end) > 0 && if (Z<0);C;else;D;end) > 0 )
-----------------------------------------------------------------------------
I don't understand this. How can an if statement itself be greater than zero? In other words, how can the if test:
if (Z > 0)
  A
else
  B
end

be greater than zero? You're not testing A or B, you're testing the whole chunk of code. But an if statement doesn't return a value. Maybe you need to write functions, like
checkAB = CheckAB(A, B); % Returns true or false.
checkCD = CheckCD(C, D); % Returns true or false.
if checkAB && checkCD
  % Do stuff
end

Subject: if statement in if clause

From: Roger Stafford

Date: 10 Oct, 2010 01:43:03

Message: 6 of 7

"Yayp Rafique" <ysheikh1984@hotmail.com> wrote in message <i8qpon$65s$1@fred.mathworks.com>...
> Hi all,
>
> I am a Java Developer and for the first time am doing the heavy lifting in MATLAB. I am writing a parser for a text file, hence some of the situations that arise might require measures that are not the most ideal but need to be done that way.
>
> My very simple question is that how can I write an if statement in the if-clause of a prior if statement. this isn't like the "easy" if statement where I can write the nested if statement in then-clause or the else-clause. i think its better if i explain through an example what I would like:
>
> if ( if (Z>0);A;else;B;end) > 0 && if (Z<0);C;else;D;end) > 0 )
> F=5
> else
> G=3
> end
>
> although this is not the correct syntax in MATLAB this is what I would like. The solution of "solving them the nested if statements before and saving their results and then using these results" will not work because I am writing a parser and I won't know the depth of nesting before hand. I have to generate a MATLAB script as the output of my parser. I would really appreciate feedback on how to do this.
- - - - - - - - - -
  I interpret that as being equivalent to the matlab code:

 if ( (Z>0 & A>0) | (~Z>0 & B>0) ) & ( (Z<0 & C>0) | (~Z<0 & D>0) )
  F = 5;
 else
  G = 3;
 end

Am I correct? If so, I believe this is the closest matlab can come to the syntax you speak of.

  Matlab must spell out logical relationships in terms of 'AND', 'OR', and 'NOT' logical operators. It does not recognize the 'if' as this kind of operator, but only as a controller of program flow.

Roger Stafford

Subject: if statement in if clause

From: Yayo

Date: 10 Oct, 2010 06:56:06

Message: 7 of 7

Hi,

Firstly, I would like to thank the community for such fast responses.

From the solutions mentioned, I personally think that I can reach a solution using Roger's example as a guideline. I ll post on this thread with my results.

Thank you Roger and everybody else.. :-)



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i8r5n7$ge$1@fred.mathworks.com>...
> "Yayp Rafique" <ysheikh1984@hotmail.com> wrote in message <i8qpon$65s$1@fred.mathworks.com>...
> > Hi all,
> >
> > I am a Java Developer and for the first time am doing the heavy lifting in MATLAB. I am writing a parser for a text file, hence some of the situations that arise might require measures that are not the most ideal but need to be done that way.
> >
> > My very simple question is that how can I write an if statement in the if-clause of a prior if statement. this isn't like the "easy" if statement where I can write the nested if statement in then-clause or the else-clause. i think its better if i explain through an example what I would like:
> >
> > if ( if (Z>0);A;else;B;end) > 0 && if (Z<0);C;else;D;end) > 0 )
> > F=5
> > else
> > G=3
> > end
> >
> > although this is not the correct syntax in MATLAB this is what I would like. The solution of "solving them the nested if statements before and saving their results and then using these results" will not work because I am writing a parser and I won't know the depth of nesting before hand. I have to generate a MATLAB script as the output of my parser. I would really appreciate feedback on how to do this.
> - - - - - - - - - -
> I interpret that as being equivalent to the matlab code:
>
> if ( (Z>0 & A>0) | (~Z>0 & B>0) ) & ( (Z<0 & C>0) | (~Z<0 & D>0) )
> F = 5;
> else
> G = 3;
> end
>
> Am I correct? If so, I believe this is the closest matlab can come to the syntax you speak of.
>
> Matlab must spell out logical relationships in terms of 'AND', 'OR', and 'NOT' logical operators. It does not recognize the 'if' as this kind of operator, but only as a controller of program flow.
>
> Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us