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:
List all "cases" in a switch programmatically

Subject: List all "cases" in a switch programmatically

From: Bjorn Gustavsson

Date: 30 May, 2013 06:39:14

Message: 1 of 6

I have a function that produces preprocessing instructions for a number of different cases. It is built with a switch command, which is all well and good. But with time the number of cases will increase to the extent that it might become difficult for a user to remember all case-labels. One way to deal with this is to make the function list the case-labels, does anyone have a way to do this programmatically?

Example function:

function output = f1(CaseName)

switch lower(CaseName)
  case 'a'
    output = {1,'well'};
  case 'b'
    output = [1.4142135];
  otherwise
    disp(str2mat({'a','b','list'}))
end

Subject: List all "cases" in a switch programmatically

From: Steven_Lord

Date: 30 May, 2013 14:34:19

Message: 2 of 6



"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
news:ko6s6i$c3i$1@newscl01ah.mathworks.com...
> I have a function that produces preprocessing instructions for a number of
> different cases. It is built with a switch command, which is all well and
> good. But with time the number of cases will increase to the extent that
> it might become difficult for a user to remember all case-labels. One way
> to deal with this is to make the function list the case-labels, does
> anyone have a way to do this programmatically?
>
> Example function:
>
> function output = f1(CaseName)
>
> switch lower(CaseName)
> case 'a'
> output = {1,'well'};
> case 'b'
> output = [1.4142135];
> otherwise
> disp(str2mat({'a','b','list'}))
> end

If you're going to have many, many labels then perhaps using a
containers.Map object or a struct-based approach would be better.

http://www.mathworks.com/help/matlab/ref/containers.mapclass.html

S = struct('a', {{1, 'well'}}, ...
    'b', sqrt(2), ...
    'c', @preprocessingFunctionC);
try
    output = S.(CaseName);
catch
    disp('Valid cases are:');
    disp(fieldnames(S));
end

Add error handling to this approach to suit.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: List all "cases" in a switch programmatically

From: Bjorn Gustavsson

Date: 31 May, 2013 06:00:25

Message: 3 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <ko7o1b$plu$1@newscl01ah.mathworks.com>...
>
>
> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
> news:ko6s6i$c3i$1@newscl01ah.mathworks.com...
> > I have a function that produces preprocessing instructions for a number of
> > different cases. It is built with a switch command, which is all well and
> > good. But with time the number of cases will increase to the extent that
> > it might become difficult for a user to remember all case-labels. One way
> > to deal with this is to make the function list the case-labels, does
> > anyone have a way to do this programmatically?
> >
> > Example function:
> >
> > function output = f1(CaseName)
> >
> > switch lower(CaseName)
> > case 'a'
> > output = {1,'well'};
> > case 'b'
> > output = [1.4142135];
> > otherwise
> > disp(str2mat({'a','b','list'}))
> > end
>
> If you're going to have many, many labels then perhaps using a
> containers.Map object or a struct-based approach would be better.
>
> http://www.mathworks.com/help/matlab/ref/containers.mapclass.html
>
> S = struct('a', {{1, 'well'}}, ...
> 'b', sqrt(2), ...
> 'c', @preprocessingFunctionC);
> try
> output = S.(CaseName);
> catch
> disp('Valid cases are:');
> disp(fieldnames(S));
> end
>
Exactly what I was looking for.

Follow-up question: Fieldname access with variables goes back to which version of Matlab? 6.2-6.5?

Thanks.

Subject: List all "cases" in a switch programmatically

From: Steven_Lord

Date: 31 May, 2013 14:16:23

Message: 4 of 6



"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
news:ko9e9p$rbo$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <ko7o1b$plu$1@newscl01ah.mathworks.com>...
>>
>> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message

*snip*

> Exactly what I was looking for.
> Follow-up question: Fieldname access with variables goes back to which
> version of Matlab? 6.2-6.5?

Dynamic field names were introduced in MATLAB 6.5 (release R13) back in
2002.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: List all "cases" in a switch programmatically

From: Bjorn Gustavsson

Date: 3 Jun, 2013 20:41:08

Message: 5 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <koabbn$rh8$1@newscl01ah.mathworks.com>...
>
>
> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
> news:ko9e9p$rbo$1@newscl01ah.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <ko7o1b$plu$1@newscl01ah.mathworks.com>...
> >>
> >> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
>
> *snip*
>
> > Exactly what I was looking for.
> > Follow-up question: Fieldname access with variables goes back to which
> > version of Matlab? 6.2-6.5?
>
> Dynamic field names were introduced in MATLAB 6.5 (release R13) back in
> 2002.
>
Good, couple of years ago one of my users were still using 6.5 so I'm safe. Does Mathworks have a public list of feature history somewhere?


Thanks for the help.
/Bjeorn

Subject: List all "cases" in a switch programmatically

From: Steven_Lord

Date: 4 Jun, 2013 14:21:33

Message: 6 of 6



"Bjorn Gustavsson" <bjonr@irf.se> wrote in message
news:koiv14$22r$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <koabbn$rh8$1@newscl01ah.mathworks.com>...
>>
>>
>> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
>> news:ko9e9p$rbo$1@newscl01ah.mathworks.com...
>> > "Steven_Lord" <slord@mathworks.com> wrote in message
>> > <ko7o1b$plu$1@newscl01ah.mathworks.com>...
>> >>
>> >> "Bjorn Gustavsson" <bjonr@irf.se> wrote in message
>>
>> *snip*
>>
>> > Exactly what I was looking for.
>> > Follow-up question: Fieldname access with variables goes back to which
>> > version of Matlab? 6.2-6.5?
>>
>> Dynamic field names were introduced in MATLAB 6.5 (release R13) back in
>> 2002.
>>
> Good, couple of years ago one of my users were still using 6.5 so I'm
> safe. Does Mathworks have a public list of feature history somewhere?

In one location? Not really. The Release Notes generally describe the major
changes to each version of MATLAB, though.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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