Stuart’s MATLAB Videos

Watch and Learn

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

Posted by Doug Hull,

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.
Other videos have been gathered here: https://blogs.mathworks.com/pick/category/video/ Other MATLAB Basics posts have been gathered here: https://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.

15 CommentsOldest to Newest

Robert McCormick replied on : 1 of 15
Ok thanks Doug for this, but which of if-else and switch-case is computationally more efficient??
Doug replied on : 2 of 15
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
Phil replied on : 3 of 15
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.
Jon replied on : 5 of 15
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
Phillip Diedrick replied on : 6 of 15
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
Doug replied on : 7 of 15
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
Rick Stauf replied on : 8 of 15
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
Rick Stauf replied on : 9 of 15
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.
Michael Claughton replied on : 10 of 15
Doug, with switch, is there a way to implement ignore case? In a string compare, you would use strcmpi, which is fine if you were using an if else, using a switch, how would you implement it? Thanks in advacnce, - Michael.
Steve Eddins replied on : 11 of 15
Michael—
switch lower(str)
  case 'string1' % enter all case strings in lower case
    do_this;
  case 'string2'
    do_this;
  otherwise
    et_phone_home;
end

Michael Claughton replied on : 12 of 15
Thanks Steve.That makes alot of sense. I appreciate it, and the matlab community. For what its worth, I ended up creating a GUI with pop-up menus, to ensure my input was always going to be the same. I do have another silly little question. I used the get command to see a list of properties of a text box, my interest being the background colour, and I noticed it was in the format [0.XXX,0.XXX,0.XXX]. Do you, or does anyone, know what format that is, and how it relates to HEX codes? Cheers. PS - I am really starting to enjoy using matlab.
Steve Eddins replied on : 13 of 15
Michael—That's an RGB triplet of floating-point values in the range [0,1]. [0,0,0] is black and [1,1,1] is white.
Nimisha Nidhi replied on : 14 of 15
Hi Doug, I need a substitute for goto statement in Matlab. Is there anything like that? More precisely, I need to code the following in Matlab:
 1  if Positive_Sentiment<0.169994 then node 2 else node 3
 2  class = Up
 3  if Positive_Sentiment<0.171732 then node 4 else node 5
 4  class = Down
 5  if Negative_Sentiment<0.021006 then node 6 else node 7
 6  class = Up
 7  if Negative_Sentiment<0.02358 then node 8 else node 9
 8  class = Down
 9  if Negative_Sentiment<0.0261051 then node 10 else node 11
10  class = Up
11  if Negative_Sentiment<0.0296845 then node 12 else node 13
12  class = Down
13  class = Up
dhull replied on : 15 of 15
While I do not understand your code, I am convinced that with a combination of IF THEN ELSE, SWITCH CASE, and other flow control in MATLAB that you do not need a GOTO. Doug