Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems using Switch

Subject: Problems using Switch

From: Lin

Date: 8 Apr, 2009 16:21:02

Message: 1 of 3

Hi all

I am using a switch for my codes, it is possible to modify the cases to something as shown instead of going through all the cases? Because case 11 || 21 || 31 will undergo the same set of operations, hence it would be troublesome to cut and paste the same thing over and over again.

path =[11 12 21 22 31 32 41 42 51 52 61 62]
for path_index = 1:length(path)

switch(path(path_index))
case 11 or 21 or 31
%perform some operation
case 12 or 22 or 42
%perform some operation
end

Thanks.

Subject: Problems using Switch

From: Sven

Date: 8 Apr, 2009 16:46:02

Message: 2 of 3

"Lin " <shenqlv@yahoo.com> wrote in message <griitd$m3l$1@fred.mathworks.com>...
> Hi all
>
> I am using a switch for my codes, it is possible to modify the cases to something as shown instead of going through all the cases? Because case 11 || 21 || 31 will undergo the same set of operations, hence it would be troublesome to cut and paste the same thing over and over again.
>
> path =[11 12 21 22 31 32 41 42 51 52 61 62]
> for path_index = 1:length(path)
>
> switch(path(path_index))
> case 11 or 21 or 31
> %perform some operation
> case 12 or 22 or 42
> %perform some operation
> end
>
> Thanks.

Hi Lin,

For a switch on numeric data, you can use an array [] to specify multiple OR cases, for a switch on character data, you can use a cell {}. See below

caseVar = 3;
switch caseVar
  case [3 4 445]
     disp '3 or 4 or 445'
  otherwise
     disp 'other number'
end

Thanks,
Sven.

Subject: Problems using Switch

From: Matt Fig

Date: 8 Apr, 2009 17:22:01

Message: 3 of 3

Using a vector is NOT correct, use a cell aray.


caseVar = 3;
switch caseVar
  case {3 4 445}
     disp '3 or 4 or 445'
  otherwise
     disp 'other number'
end

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us