Loren on the Art of MATLAB

Turn ideas into MATLAB

A Case for Divergence between MATLAB and C

There are some functions in MATLAB that we designed to be similar to their counterparts in other languages. There are also some functions that we deliberately designed differently. A case in point is the syntax and behavior for the switch statement.


No Fall Through in MATLAB

The individual case statements in MATLAB do not fall through to the next case, the default behavior in C. We found that in C, one common source of bugs was not properly stopping the default fall through behavior but termination with a break statement. So we eliminated the need to terminate each case in MATLAB.

Why Does C Allow Fall Through?

At least one major reason C allows fall through behavior is so that multiple cases can share code. This is generally very good for code hygiene. Otherwise, you risk code bloat and propogation of errors via copy-paste, among other pitfalls.

How Does MATLAB Handle Code Sharing Between Cases?

MATLAB avoids the code bloat and error propagation downsides of no fall through behavior by allowing you to group cases together, in a cell array. In fact, if you want to share code, you must group like cases together since you cannot fall through to another case under any circumstances. If your expression matches more than one case, the first one encountered in the switch is the one that will execute.

If you have some cases that share most of the code in common but still differ a bit, you have several options. You can place them in the same case and then embed another switch statement or use conditional logic, via if statements. Or write code perhaps using appropriate logical indexing.

What About Default?

Finally, we felt that the last choice in a switch statement, being called default, was a bit misleading. It is used if no other case matches, but that is rarely the behavior the programmer is expecting. Instead, we chose otherwise because we felt it better reflected that none of the previous cases matched at all.

Example Code

Here's a simple example that shows the features of switch.

type blogswitch
function blogswitch(myinput)
% Sample switch statement for blog

switch myinput
    case {1, true, 'yes','true'}
        disp('Input is true')
    case {0, 'false', 'no', false}
        disp('Input is false')
    case {ones, zeros}
        disp('Ya can''t get here from there.  You need to make up your mind!')
        disp('No dice')

One thing you may notice is that you can't reach the third case. The case listed with {ones, zeros} will always be satisfied by one of the first two cases and won't ever execute.

Let's try the code.

blogswitch true
Input is true
blogswitch maybe
No dice

What Cases Do You Run Into?

I wonder what situations you find yourselves in. Do you use switch statements with cases for multiple conditions? Did you even know you could do that? Let me know your switch-case experiences here.

Published with MATLAB® 7.9



To leave a comment, please click here to sign in to your MathWorks Account or create a new one.