Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switch statement with cell arrays

Status
Not open for further replies.

deaner00

Mechanical
Oct 31, 2006
2
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.
 
Replies continue below

Recommended for you

Can't you use an if statement.

if (i>=4) && (i=<100)
display('?')
else
break

Or somehting to that effect.

That do what you are looking for?

BsK
 
Thank you for the reply. An if wouldn't work though. I have a large switch statement with about 20 string case statements. I needed to add in one case statement that encompassed the numbers 3-100, and had to be in the same switch. I ended up figuring it out though. If anyone runs into this you,

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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor