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.
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.
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,
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.
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
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.
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?
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.
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.
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.
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
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
Leave a Reply
About
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
Ok thanks Doug for this, but which of if-else and switch-case is computationally more efficient??
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
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.
Phil,
Excellent example. Thank you!
Doug
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
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
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
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
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.
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.
Michael—
switch lower(str) case 'string1' % enter all case strings in lower case do_this; case 'string2' do_this; otherwise et_phone_home; endThanks 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.
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.
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:
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