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.
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.
I’ve got a question about this string. Before this is a whole m-script with the needed parameterinputs, and there is no problem with them. It’s this particular part, where i need to get the 2000>REwater>3000 to work. But I believe this is not a right command for a Matlab m-file. Do you know an alternative to get REwater be unknown when the Reynoldsnumber is larger than 2000 but less than 3000?
Thanks in advance!
REwater=(roowater*v*D)/etawater
if REwater3000
disp(’The liquid is turbulent’)
elseif 2000>REwater<3000
disp(’The liquid of water is unknown, laminair and turbulent’)
end
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.
I’ve got a question about this string. Before this is a whole m-script with the needed parameterinputs, and there is no problem with them. It’s this particular part, where i need to get the 2000>REwater>3000 to work. But I believe this is not a right command for a Matlab m-file. Do you know an alternative to get REwater be unknown when the Reynoldsnumber is larger than 2000 but less than 3000?
Thanks in advance!
REwater=(roowater*v*D)/etawater
if REwater3000
disp(’The liquid is turbulent’)
elseif 2000>REwater<3000
disp(’The liquid of water is unknown, laminair and turbulent’)
end
I think instead of
if (x < y < z)
you want
if (x < y) & (y < z)
The first construct could be done with numbers like this:
>> 0 < 0.5 < 1
ans =
0
(I suspect you expected the statement to be true!)
Which has an implied parenthesis here:
(0 < 0.5) < 1
Since (0 < 0.5) evaluates to true (1) this means
1 < 1
Which evaluates to false (0)
Try my construct:
>> (0 < 0.5) & (0.5 < 1)
ans =
1
And I think you will get what you were expecting.
-Doug
Do the switch/case commands go in the function file or the script file.
Is there a way to automatically do the switch command or do you have to ask the user to input the value that you want to switch.
Carly,
Switch/case can be used in functions or scripts.
The switch command can work on any variable, not just one put in by the user.
Doug