×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Switch statement with cell arrays

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.

RE: Switch statement with cell arrays

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

RE: Switch statement with cell arrays

(OP)
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.  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources