2017-01-23 802 views
4

我读this代码和行97,我发现下面的代码:在Matlab开关/ case中的空语句?

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

我从来没有见过空语句(根据documentation)。这是什么意思?

“如果lower(opts.color)要么是rgbopponent然后做...

“如果lower(opts.color)rgb什么也不做,如果它是opponent...”?

回答

8

如果case块为空,则不会为该特定情况执行任何操作。所以如果opt.colors'rgb'不采取任何行动。

的原因,笔者甚至懒得把它作为一个case是因为如果他们没有,则otherwise块中的代码(这台opts.color'hsv'因为所提供的色彩空间无法识别/有效)将如果opt.colors'rgb',那么显然是不受欢迎的行为。

该块是

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

功能等价物为一个case块相匹配的几个值的语法要求使用cell array for the case expression的。

switch lower(opts.color) 
    case {'rgb', 'opponent'} 
     ... 
end 
+0

非常感谢! :) – justHelloWorld