Thread Subject: Mixed type switch/case

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 15:16:01

Message: 1 of 12

I get an m-lint warning because (string and char are mixted) when doing this

switch myoption
   case {1, 'option1'}
      % do something ...
   case {2, 'option2'}
      % do something else
end

However the statement seems to work as expected.

Can someone elaborate the reason or find an count-example why mlint is right?

Bruno

Subject: Mixed type switch/case

From: Matt Fig

Date: 16 Nov, 2009 15:27:02

Message: 2 of 12

In 2006a and 2007b, I get no M-Lint for this:

function [] = switcher(myoption)

switch myoption
   case {1, 'option1'}
      disp('Inside Case 1')
   case {2, 'option2'}
      disp('Inside Case 2')
    otherwise
      disp('Inside Case Other')
end



What version are you using?

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 15:33:04

Message: 3 of 12

Ah, incomplete post, try this Matt

switch lower(myoption) % <- change here
   case {1, 'option1'}
      disp('Inside Case 1')
   case {2, 'option2'}
      disp('Inside Case 2')
    otherwise
      disp('Inside Case Other')
end

Mine is 2009B

Bruno

Subject: Mixed type switch/case

From: Matt Fig

Date: 16 Nov, 2009 15:51:05

Message: 4 of 12

TMW must have changed something. I do not understand why you get this, because the double 1 is a valid output from the lower function. I.e., lower(1)==1. It seems that as long as a legitimate output from the lower function is in the case (at least), I get no warning.


function [] = switcher(myoption)

switch lower(myoption)
    case {1,'option1',uint8(1),true,gca}
        disp('Inside Case 1')
    case {'option2',2,false,gcf}
        disp('Inside Case 2')
    otherwise
        disp('Inside Case Other')
end

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 16:07:01

Message: 5 of 12

The mlint message is
"This SWITCH statement has a mix numerical and string values that is unlikelu to work correctly"

Of course there is no reason LOWER create problem, since it does not change numerical input as this example shows:

lower('A')=='A' % false

lower(double('A'))=='A' % true
char(lower(double('A'))) % 'A'

So to me MLINT just issues an unjustified warning in this case.

Thanks,

Bruno

Subject: Mixed type switch/case

From: Matt Fig

Date: 16 Nov, 2009 16:12:03

Message: 6 of 12

I guess you will have to wait until:

expert1 = 'SL'

or

expert2 = fliplr(expert1)

logs on to straighten it out.

Subject: Mixed type switch/case

From: Doug Schwarz

Date: 16 Nov, 2009 16:56:28

Message: 7 of 12

In article <hdrtki$ekg$1@fred.mathworks.com>,
 "Matt Fig" <spamanon@yahoo.com> wrote:

> I guess you will have to wait until:
>
> expert1 = 'SL'
>
> or
>
> expert2 = fliplr(expert1)
>
> logs on to straighten it out.


If you run this, is it obvious what the correct result should be?


myoption = 'A';
switch myoption
    case {64,'A'}
        disp(1)
    case {65,'B'}
        disp(2)
end
 
 
myoption = 65;
switch myoption
    case {64,'A'}
        disp(1)
    case {65,'B'}
        disp(2)
end


I assume mlint is trying to warn about this possible confusion.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 17:18:02

Message: 8 of 12

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-6805DC.11562816112009@news.frontiernet.net>...
>
> If you run this, is it obvious what the correct result should be?

OK I'll try to guess without runing Matlab

>
>
> myoption = 'A';
> switch myoption
> case {64,'A'}
> disp(1) <---------------
> case {65,'B'}
> disp(2)
> end
>

>
> myoption = 65;
> switch myoption
> case {64,'A'}
> disp(1) <--------------
> case {65,'B'}
> disp(2)
> end
>
>
> I assume mlint is trying to warn about this possible confusion.

Am I right?

Bruno

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 17:29:03

Message: 9 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> Am I right?
>

Yes I am.

Furthermore, if this was the reason, I wonder then mlint does not issue the warning when LOWER is not presented on SWITCH statement? Something is still not consistent.

Bruno

Subject: Mixed type switch/case

From: Steven Lord

Date: 16 Nov, 2009 18:44:13

Message: 10 of 12


"Matt Fig" <spamanon@yahoo.com> wrote in message
news:hdrtki$ekg$1@fred.mathworks.com...
>I guess you will have to wait until:
>
> expert1 = 'SL'
>
> or
>
> expert2 = fliplr(expert1)
>
> logs on to straighten it out.

I'm not certain why that message is being triggered. I'm asking around.

Just to make sure I'm not duplicating some effort that's already taking
place ... have you sent this in to Technical Support yet?

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Mixed type switch/case

From: Bruno Luong

Date: 16 Nov, 2009 19:00:19

Message: 11 of 12

"Steven Lord" <slord@mathworks.com> wrote in message <hds6fr$19l$1@fred.mathworks.com>...
>
> "Matt Fig" <spamanon@yahoo.com> wrote in message
> news:hdrtki$ekg$1@fred.mathworks.com...
> >I guess you will have to wait until:
> >
> > expert1 = 'SL'
> >
> > or
> >
> > expert2 = fliplr(expert1)
> >
> > logs on to straighten it out.
>
> I'm not certain why that message is being triggered. I'm asking around.
>
> Just to make sure I'm not duplicating some effort that's already taking
> place ... have you sent this in to Technical Support yet?

Not yet Steve. Thanks for taking care of this mlint messaga.

Bruno

Subject: Mixed type switch/case

From: Steven Lord

Date: 18 Nov, 2009 18:18:37

Message: 12 of 12


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
news:hds7g3$502$1@fred.mathworks.com...
> "Steven Lord" <slord@mathworks.com> wrote in message
> <hds6fr$19l$1@fred.mathworks.com>...
>>
>> "Matt Fig" <spamanon@yahoo.com> wrote in message
>> news:hdrtki$ekg$1@fred.mathworks.com...
>> >I guess you will have to wait until:
>> >
>> > expert1 = 'SL'
>> >
>> > or
>> >
>> > expert2 = fliplr(expert1)
>> >
>> > logs on to straighten it out.
>>
>> I'm not certain why that message is being triggered. I'm asking around.
>>
>> Just to make sure I'm not duplicating some effort that's already taking
>> place ... have you sent this in to Technical Support yet?
>
> Not yet Steve. Thanks for taking care of this mlint messaga.

The developers I spoke to have taken a quick look at this and it does appear
like this isn't behaving as it should. They've entered it into the database
for further investigation about whether the code pattern that triggers this
message should be modified or if it's the message itself that should be
changed to more clearly describe M-Lint's concern and how to correct it.

From the discussion I had with the developers, I believe the intent of this
message was to flag cases where the SWITCH expression and the CASE
expression were "the same" but different types, like:


x = 2100;
switch x
    case {'2100', '2200', '2300'}
        handleNonLeapMultiplesOf100;
    % other cases
end


In this scenario, handleNonLeapMultiplesOf100 will not be executed, as
though the case _appears_ to be the same as the SWITCH expression they are
of different types.

Thanks for flagging this.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com