Thread Subject: switch/case vs if/then

Subject: switch/case vs if/then

From: Travis

Date: 11 May, 2009 03:29:58

Message: 1 of 7

I have a number of variables in a structure that are either 1 or 0. Right now I know how to use if/then statements to accomplish what I want, but would switch/case be quicker, more efficient, at doing the same thing? and how exaclty would I code it in?

so right now my code is...

if struct.var == 1
  do something
end
repeat for all the variables

Subject: switch/case vs if/then

From: Alric Gta

Date: 11 May, 2009 04:32:01

Message: 2 of 7

"Travis " <sinusoid2@hotmail.com> wrote in message <gu863m$nag$1@fred.mathworks.com>...
> I have a number of variables in a structure that are either 1 or 0. Right now I know how to use if/then statements to accomplish what I want, but would switch/case be quicker, more efficient, at doing the same thing? and how exaclty would I code it in?
>
> so right now my code is...
>
> if struct.var == 1
> do something
> end
> repeat for all the variables


Just do:

if ( if struct.var == 0)
  option=0;
 end

if ( if struct.var == 1)
  option=1;
 end

switch option
   case 0
      do what ever yopu want to do when struct.var==0
   case 1
      do what ever yopu want to do when struct.var==0

end

Subject: switch/case vs if/then

From: Alric Gta

Date: 11 May, 2009 04:32:02

Message: 3 of 7

"Travis " <sinusoid2@hotmail.com> wrote in message <gu863m$nag$1@fred.mathworks.com>...
> I have a number of variables in a structure that are either 1 or 0. Right now I know how to use if/then statements to accomplish what I want, but would switch/case be quicker, more efficient, at doing the same thing? and how exaclty would I code it in?
>
> so right now my code is...
>
> if struct.var == 1
> do something
> end
> repeat for all the variables


Just do:

if ( if struct.var == 0)
  option=0;
 end

if ( if struct.var == 1)
  option=1;
 end

switch option
   case 0
      do what ever yopu want to do when struct.var==0
   case 1
      do what ever yopu want to do when struct.var==0

end

Subject: switch/case vs if/then

From: Travis

Date: 11 May, 2009 05:02:01

Message: 4 of 7

"Alric Gta" <alric2rei@yahoo.com> wrote in message <gu89o2$nab$1@fred.mathworks.com>...
> "Travis " <sinusoid2@hotmail.com> wrote in message <gu863m$nag$1@fred.mathworks.com>...
> > I have a number of variables in a structure that are either 1 or 0. Right now I know how to use if/then statements to accomplish what I want, but would switch/case be quicker, more efficient, at doing the same thing? and how exaclty would I code it in?
> >
> > so right now my code is...
> >
> > if struct.var == 1
> > do something
> > end
> > repeat for all the variables
>
>
> Just do:
>
> if ( if struct.var == 0)
> option=0;
> end
>
> if ( if struct.var == 1)
> option=1;
> end
>
> switch option
> case 0
> do what ever yopu want to do when struct.var==0
> case 1
> do what ever yopu want to do when struct.var==0
>
> end

Would this be worth doing as it appears to add more lines into the code as opposed to cleaning it up?

Subject: switch/case vs if/then

From: Darren Rowland

Date: 11 May, 2009 05:06:01

Message: 5 of 7

Loren wrote recently about the switch statement in Matlab, http://blogs.mathworks.com/loren/2009/04/28/switching-things-up/

Another option here depends on whether your (do something) can be made to use logical operations directly, such as in logical indexing. You would likely obtain better code than bulky switch statements.

Subject: switch/case vs if/then

From: Travis

Date: 11 May, 2009 06:03:01

Message: 6 of 7

"Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message <gu8bnp$7ie$1@fred.mathworks.com>...
> Loren wrote recently about the switch statement in Matlab, http://blogs.mathworks.com/loren/2009/04/28/switching-things-up/
>
> Another option here depends on whether your (do something) can be made to use logical operations directly, such as in logical indexing. You would likely obtain better code than bulky switch statements.

OK, so the final code would look like this

if ToolOptions.APEAK == 1;
    apeak = interp2(X,Y,Z,Xi,Yi)
end
if ToolOptions.BPEAK == 1
    bpeak = interp2(X,Y,Z,Xii,Yii)
end
etc. (about 10 more times)

Subject: switch/case vs if/then

From: Steven Lord

Date: 11 May, 2009 13:39:54

Message: 7 of 7


"Travis " <sinusoid2@hotmail.com> wrote in message
news:gu8f2l$gbl$1@fred.mathworks.com...
> "Darren Rowland" <darrenjremovethisrowland@hotmail.com> wrote in message
> <gu8bnp$7ie$1@fred.mathworks.com>...
>> Loren wrote recently about the switch statement in Matlab,
>> http://blogs.mathworks.com/loren/2009/04/28/switching-things-up/
>>
>> Another option here depends on whether your (do something) can be made to
>> use logical operations directly, such as in logical indexing. You would
>> likely obtain better code than bulky switch statements.
>
> OK, so the final code would look like this
>
> if ToolOptions.APEAK == 1;
> apeak = interp2(X,Y,Z,Xi,Yi)
> end
> if ToolOptions.BPEAK == 1
> bpeak = interp2(X,Y,Z,Xii,Yii)
> end
> etc. (about 10 more times)

x = 1;
S = whos('x');
FN = fieldnames(S);
for k = 1:length(FN)
    fprintf('Processing field %s of S.\n', FN{k});
    disp(S.(FN{k}));
end

Alternately you could use STRUCTFUN.

This assumes, of course, that you can generalize from what field you're
processing to what operation you want to perform. I'm not sure if you're
going to be able to apply it in the case you described above, as deciding
what variables to use for the output of INTERP2 and the last two inputs may
still require a SWITCH statement or a bunch of IF/ELSE statements.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
case Travis 10 May, 2009 23:34:05
then switch Travis 10 May, 2009 23:34:05
if Travis 10 May, 2009 23:34:04
rssFeed for this Thread

Contact us at files@mathworks.com