2017-01-23 806 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