Switch statement with cell arrays
Switch statement with cell arrays
(OP)
I want to use a switch statement with a cell array as the case expression. I know it can be used with few expressions like this:
switch lower(method)
case {'linear','bilinear'}
disp('Method is linear')
But I have a fairly lengthy switch statement where one case needs to look for numbers 4 through 100:
switch number
case {'4', '5', ...'99', '100'}.
disp('display something')
Does someone know how to make this work without writing out all 97 numbers?
Thanks for your help.
switch lower(method)
case {'linear','bilinear'}
disp('Method is linear')
But I have a fairly lengthy switch statement where one case needs to look for numbers 4 through 100:
switch number
case {'4', '5', ...'99', '100'}.
disp('display something')
Does someone know how to make this work without writing out all 97 numbers?
Thanks for your help.





RE: Switch statement with cell arrays
if (i>=4) && (i=<100)
display('?')
else
break
Or somehting to that effect.
That do what you are looking for?
BsK
RE: Switch statement with cell arrays
First, define and convert your cell array in one for loop before the switch. (convert to a string in order to be used in switch statement):
for n=3:100
num{n}=num2str{n};
end
Then, in the case statement you input the complete cell array:
case num %no curly braces or parens
statement;
otherwise
statement; ...
Works fine.