2017-07-25 86 views

回答

0

您可以使用strcmpi在MATLAB中不區分大小寫的比較,但您希望使用if語句代替開關...

% If you must use the 's' flag for input, it is directly stored as a string 
% so you don't have to input the quotation marks! 
str = input('Enter string', 's'); 

if strcmpi(str, 'VALUE') 
    % true for 'VAlue', 'VALUe', 'valUE', ... 
elseif strcmpi(str, 'anothervalue') 
    % true for 'AnotherValue', 'ANOTHERvalue', ... 
else 
    % Anything else 
end 

實際上,你可以一個單元陣列上使用strcmpi,所以你並不需要在所有的if報表(根據您的使用)。

% Define your test strings 
mystrings = {'abc', 'def', 'ghi', 'jkl'}; 
% Set text (could do via input) 
str = 'def'; 
% Compare ALL 
choice = strcmpi(str, mystrings); 
>> choice = [0 1 0 0] % logical vector output 

所以,如果你組織你的代碼有基質或細胞內的相關操作,那麼你可以使用這個choice輸出作爲選擇,加快/由沒有做任何測試的情況下,簡化您的代碼。

相關問題