Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

File Exchange Pick of the Week

January 2nd, 2008

MATLAB Basics: ‘Switch case’ vs. ‘If elseif’

This three minute video takes a look at the “Switch case” flow control statement and contrasts it with the more familiar “If elseif” flow control statement.

Often times, people will use an “If elseif” statement where a “Switch case” statement is going to be cleaner and easier to understand and maintain.

Video Content

Find the files here.
PodCast here

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

Other MATLAB Basics posts have been gathered here:
http://blogs.mathworks.com/pick/category/matlab-basics/

As a cool little post script: At most places you celebrate the 100th something or other, but since this is a software company I feel I should celebrate the 64th video (2^6) and as I post this I see that it is the 18128th file on the File Exchange capturing the next power of two in the last three digits.

9 Responses to “MATLAB Basics: ‘Switch case’ vs. ‘If elseif’”

  1. Robert McCormick replied on :

    Ok thanks Doug for this, but which of if-else and switch-case is computationally more efficient??

  2. Doug replied on :

    Robert,

    I tested, and it really depended on the number of iterations. For reasonable numbers of iterations it did not seem to matter. TIC TOC and the profiler can be a big help here.

    When asked about these kinds of things, I often say “Just code it”, run the profiler and see where the problems are. There is no need to try and optimize one second off of this kind of statement when there is some calculation that take 300 seconds anyways. Write your code clean and maintainable, then modify the slow bits as they appear.

    Doug

  3. Phil replied on :

    Doug,
    I often see programmers rely on the ‘if elseif’ chain in order to determine if a variable (n) falls into one of several ranges. MATLAB help is not clear on how this is done using the switch statement, but it is possible by comparing the logical value true against each of the range evaluations…

    n=80
    switch logical(true)
    case n>100, disp(’high’);
    case n>50, disp(’moderate’);
    case n>0, disp(’low’);
    otherwise disp(’invalid’);
    end;

    Again, this is a much more elegant solution than the ‘if elseif’ alternative.

    Phil.

  4. Doug replied on :

    Phil,

    Excellent example. Thank you!

    Doug

  5. Jon replied on :

    Phil,

    Thanks for that! Exactly what I was looking for. MATLAB help does kind of fail to explain how to use logicals with a switch statement.

    Jon

  6. Phillip Diedrick replied on :

    I want to use the switch/case for the following. I want to prompt the user to input one the following strings: triangle, square,pentagon or hexagon. Depending which figure you input the program will then calculate the sum of the interior angles of the figure using (n-2)(180). I tried the following and I’m prompted with an error message. Here is my code.
    clear;
    clc;
    n=input(’Please input the figure:’)
    switch n
    case (’triangle’)
    n=3;
    sum=(n-3).*(180)
    case (’square’)
    n=4;
    sum=(n-3).*(180)
    case(’pentagon’)
    n=5;
    sum=(n-3).*(180)
    case (’hexagon’)
    n=6;
    sum=(n-3).*(180)
    end
    Please could you assist me.
    Thanks

  7. Doug replied on :

    Phillip,

    You need this for your input statement:

    n=input(’Please input the figure:’,’s’)

    This interprets whatever is typed as a string. Without that switch, MATLAB is taking what you type and interpreting it and thus looks for a function called “triangle” or whatever and does not find it.

    Doug

  8. Rick Stauf replied on :

    Doug,

    I’m looking for a GoTo function for Matlab. I have an efficient search routine that I like to use in a number of areas that requires GoTos. Is there anything like this in matlab procedures?

    Thank you for your help,
    Rick

  9. Rick Stauf replied on :

    Never Mind, I found a more efficient way to do it. Instead of searching the time area I simply multiply the time by the delta time to acquire the index number in the array. Thanks for your web tutorials. They are very helpful.

Leave a Reply


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • panweichen: hello, My name is Pan. I am sending you this E-mail to look for the possibility to use Toolbox to...
  • Gamal: Thank u Doug for ur fast reply. yes I have an NI-DAQ USB 6008. and I have an Analogue voltage sinusoidal...
  • Hector Mejía: Hi, I have Matlab 7.5.0 (R2007b) and I need to create a table on an interface, but in the GUI...
  • Steve L: DH Cho, Use a cell array instead of growing the vector/matrix inside the loop. fileName = 'a.xls'; range =...
  • JJ Garza: Tutorial gave very helpful insight to my problem, but it seems to have generated a new problem in my case....
  • nilu: it’s good
  • Quan Quach: Hey, that is a pretty nifty use of MATLAB! I’m going to make an alarm system for my house.
  • Rodney Thomson: I was most dismayed when I saw that as of 2008/10/07 only 178 people had downloaded this package from...
  • Doug: Gamal, That is something that can be done. Do you have the Data Acquisition toolbox? What have you tried, can...
  • Gamal: Dear Sir, I am working in a project that i use an Analogue to digital converter HW to transfer analogue...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics