Comments on: Switching Things Up https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Thu, 19 Nov 2009 11:54:51 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30820 Thu, 19 Nov 2009 11:54:51 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30820 OysterEngineer-

if-else will result in the same complexity as switch. We often make the same trade-off you do, to keep a switch statement instead of having some very small subfunctions. There is a reason switch drives up the complexity though. The number of possible paths through the code does indeed go up, just as in an if-else situation.

–Loren

]]>
By: OysterEngineer https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30815 Thu, 19 Nov 2009 01:02:12 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30815 I like the switch syntax since the code is so easy for most people to read.

However, I don’t like how it “artificially” drives the McCabe Clclomatic complexity index high. Thus, when I document a function that I’ve written, & include the output from Code Metrics, I have to write some text to discuss why, for example, the complexity index is 14 rather than my target of less than 10.

In many cases, my co-workers & I look at the function in detail & decide that it is clearer to use the switch-case syntax & accept the higher index rather than dividing the function into sub-functions just to get the index below 10.

I’m under the impression that using if-ifelse syntax will result in the same complexity index as the switch-case syntax. Yet, the switch-case syntax is usually clear.

Maybe I need to invent an Oyster Complexity Index that excludes the effect of the switch-case syntax.

I appreciate Iain’s discussion of decision tables from Code Complete. I browsed thru our copy & see how to implement such a thing in readable code.

]]>
By: Loren https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30631 Tue, 22 Sep 2009 02:15:47 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30631 locolab-

yes, you can use conditional statements for cases in a switch statement.

–Loren

]]>
By: locolab https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30630 Mon, 21 Sep 2009 21:17:30 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30630 Can you use conditional statments in a switch statment for example:

switch num
case num<-2
disp(‘Num less than -2’)
otherwise
disp(‘Error’)
end

]]>
By: Jim Weiner https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30299 Fri, 08 May 2009 20:31:29 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30299 A not very clearly documented feature of the switch/case construct that I’ve found useful is that it’s much more general than the examples might lead you to believe. All the examples I’ve seen have been of the form:

switch x
case v1

case v2


end

where x is a variable or expression, and each of the vi are numeric or string constants or cells arrays comprised thereof.

But this also works:

switch true
case exp1

case exp2


end

where each of the expi are logical expressions. The first that evaluates as true has its branch executed.

]]>
By: Joe Kirk https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30290 Thu, 07 May 2009 11:52:04 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30290 Chris,

I like your method for some applications.

In the particular case I showed however, I am doing more than a simple table lookup. Instead, I am trying to take an input string of color characters and build an Mx3 matrix of RGB triplets.

For example, if someone provides the input ‘rygb’, I want to build the matrix [1 0 0; 1 1 0; 0 1 0; 0 0 1].

The colors need to be in the correct order, and if someone provides an invalid color character, I want to ignore it.

To achieve this with your method, I need to do the following:

color_table = {
    'r',    [1 0 0]     % red
    'g',    [0 1 0]     % green
    'b',    [0 0 1]     % blue
    'y',    [1 1 0]     % yellow
    'c',    [0 1 1]     % cyan
    'm',    [1 0 1]     % magenta
    'p',    [1 0 .5]    % pink
    'o',    [1 .5 0]    % orange
    'l',    [.5 1 0]    % lime green
    'a',    [0 1 .5]    % aquamarine
    's',    [0 .5 1]    % sky blue
    'v',    [.5 0 1]    % violet
    'k',    [.5 .5 .5]  % grayscale
    'w',    [.5 .5 .5]  % grayscale
    };

num_clrs = length(input(:));
clr_mat = zeros(num_clrs,3);
c = 0;
for k = 1:num_clrs
    c = c + 1;
    ii = find(strcmp(color_table(:,1),lower(input(k))));
    if ~isempty(ii)
        clr_mat(c,:) = color_table{ii,2};
    else
        c = c - 1;
    end
end
clr_mat = clr_mat(1:c,:);
if ~isempty(clr_mat)
    clrs = clr_mat
end

In my opinion, my SWITCH statement is more compact and readable. I may use your method for other things though… :o)

]]>
By: Cris Luengo https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30272 Tue, 05 May 2009 15:15:57 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30272 Rather than Joe, Iain or Eric’s way of doing look-up tables, I like the clarity of cell arrays and the strcmp function:

color_table = {
'red',   [1 0 0]
'green', [0 1 0]
'blue',  [0 0 1]
};
ii = find(strcmp(color_table(:,1), name ));
clr_mat(c,:) = color_table{ii,2};

.

The problem with containers.Map, in my opinion, is that the key and the value are physically separated. If you want to add one value to the table, you need to edit your function in two places.

The switch statement really shines when you need to execute different bits of code based on an input, it’s not so useful for a simple table lookup.

]]>
By: Iain https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30261 Thu, 30 Apr 2009 11:13:59 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30261 I have been using Malab for six years and had no idea it had a switch statement. I have always used elseif, which is a more flexible alternative. I think I will probably stick with elseif in a lot of cases.

In the simplest cases, like Eric, I often replace long switch statements with lookup tables. I didn’t know that Matlab finally ships with a decent map data structure. The old-style way of doing Joe Kirk’s example would be to put the colors in a structure:
color_table.r = [1 0 0]; % red
color_table.g = [0 1 0]; % green
...
and then lookup like this:
clr_mat(c,:) = color_table.(lower(input(k)));One would have to test for field existence or catch exceptions if unknown colors might be requested.

Of course, which approach to use is a stylistic choice and will depend on the precise situation. The book “Code Complete” has a section on “Using decision tables to replace complicated logic” that is worth considering.

]]>
By: Loren https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30259 Wed, 29 Apr 2009 17:56:31 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30259 Kieran-

You could switch on the combined tests by converting them to “binary”:

logtest = logtest1*2+logtest1

switch logtest
   case 0
   case 1
   case 2
   case 3
end

I don’t know if that’s more readable or less so.

–Loren

]]>
By: Kieran https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30258 Wed, 29 Apr 2009 17:47:47 +0000 https://blogs.mathworks.com/loren/2009/04/28/switching-things-up/#comment-30258 I sometimes use switch statements in the following (somewhat kludgy) manner:

switch sprintf('%d %d', logical_test1, logical_test2)
  case '0 0'
  % Do A
  case '0 1'
  % Do B
  case '1 0'
  % Do C
  case '1 1'
  % Do D
end

This avoid a bunch of nested if statements and is somewhat clearer in my mind than:

if logical_test1 
   if logical_test2
     % Do D
   else
     % Do C
   end
else
   if logical_test2
    % Do B
   else
    % Do A
   end
end

I’m open to suggestion to improve this.

]]>