Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Nested if statements in MATLAB

  1. Sep 25, 2013 #1
    Just started a new college module and we'll be using MATLAB quite a bit. I was wondering, can you nest if statements in MATLAB like in C#?
    Would code such as this work in MATLAB?

    if (condition 1)
    if (sub condition 1)
    Code
    elseif (sub condition 2)
    Code
    elseif (condition 2)
    if (sub condition 3)
    Code
    elseif (sub condition 4)
    Code
    end
     
  2. jcsd
  3. Sep 25, 2013 #2

    kreil

    User Avatar
    Gold Member

    Yes, of course.
    Code (Text):

    a = 6;

    if abs(a) <= 5
        if a < 0
            a = -a;
        elseif a > 3
            a = a -2;
        end
    elseif abs(a) > 5
        if a > 10
            a = a - 5;
        elseif a < 7
            a = a -2;
        end
    end

    a
    a =

         4
     
  4. Sep 25, 2013 #3
    Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Nested if statements in MATLAB
  1. Matlab if statements (Replies: 1)

Loading...