Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty statement in Matlab switch/case?

I was reading this code and at line 97 I found the following code:

switch lower(opts.color)
  case 'rgb'
  case 'opponent'
  ...

I've never seen empty statements (according to documentation). What does it mean?

"If lower(opts.color) is either rgb or opponent then do ..."

or

"If lower(opts.color) is rgb do nothing and if it's opponent do ..."?

like image 943
justHelloWorld Avatar asked Jan 23 '17 18:01

justHelloWorld


1 Answers

If the case block is empty, then nothing is executed for that particular case. So if opt.colors is 'rgb' no action is taken.

The reason that the author has even bothered to include it as a case is because if they hadn't, the code within the otherwise block (which sets opts.color to 'hsv' because the supplied colorspace wasn't recognized/valid) would be executed if opt.colors was 'rgb' which obviously is undesirable behavior.

The block is the functional equivalent of

if ~strcmpi(opts.color, 'rgb')
    switch lower(opts.color)
        case 'opponent'
            % Do stuff
        case 'hsv'
            % Do other stuff
        otherwise
            % Throw warning
    end
end

The syntax for a case block which matches several values requires the use of a cell array for the case expression.

switch lower(opts.color)
    case {'rgb', 'opponent'}
        ...
end
like image 135
Suever Avatar answered Sep 21 '22 15:09

Suever