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:
SWITCH expression ERROR !!!!

Subject: SWITCH expression ERROR !!!!

From: Serhat

Date: 22 Jan, 2013 22:54:09

Message: 1 of 7

Hi,

I want to plot 3 graphics successively. So I use this code:

for h=1:3
switch h
case 1
        semilogy(x, y, 'rx-')
case 2
        semilogy(x, y, 'bo-')
case 3
        semilogy(x, y, 'md-')
end

but it gives this message "" SWITCH expression must be a scalar or string constant."

What can I do? Thanks in advance:)

Subject: SWITCH expression ERROR !!!!

From: Sadik

Date: 22 Jan, 2013 23:08:11

Message: 2 of 7

Hi Serhat,

I don't think you pasted your code thoroughly. My guess is that you are making some modification to h within the loop before the switch statement and then it becomes a vector.

Best.

Subject: SWITCH expression ERROR !!!!

From: anyone

Date: 22 Jan, 2013 23:50:28

Message: 3 of 7

On Tue, 22 Jan 2013 22:54:09 +0000, Serhat wrote:

> I want to plot 3 graphics successively. So I use this code:
> for h=1:3
> switch h
> case 1
> semilogy(x, y, 'rx-')
> case 2
> semilogy(x, y, 'bo-')
> case 3
> semilogy(x, y, 'md-')
> end
> but it gives this message "" SWITCH expression must be a scalar or
> string constant."

well, it -should- have told you your 'for' loop doesn't end.

> What can I do?

Forget 'case', try invoking 'semilogy' three times, either as stacked figures ("tall"), or as three separate figures.

>Thanks in advance:)

read 'help' in advance.

Subject: SWITCH expression ERROR !!!!

From: anyone

Date: 22 Jan, 2013 23:49:28

Message: 4 of 7

On Tue, 22 Jan 2013 22:54:09 +0000, Serhat wrote:

> I want to plot 3 graphics successively. So I use this code:
> for h=1:3
> switch h
> case 1
> semilogy(x, y, 'rx-')
> case 2
> semilogy(x, y, 'bo-')
> case 3
> semilogy(x, y, 'md-')
> end
> but it gives this message "" SWITCH expression must be a scalar or
> string constant."

well, it -should- have told you your 'for' loop doesn't end.

> What can I do?

Forget 'case', try invoking 'semilogy' three times, either as stacked figures ("tall"), or as three separate figures.

>Thanks in advance:)

read 'help' in advance.

Subject: SWITCH expression ERROR !!!!

From: anyone

Date: 22 Jan, 2013 23:49:58

Message: 5 of 7

On Tue, 22 Jan 2013 22:54:09 +0000, Serhat wrote:

> I want to plot 3 graphics successively. So I use this code:
> for h=1:3
> switch h
> case 1
> semilogy(x, y, 'rx-')
> case 2
> semilogy(x, y, 'bo-')
> case 3
> semilogy(x, y, 'md-')
> end
> but it gives this message "" SWITCH expression must be a scalar or
> string constant."

well, it -should- have told you your 'for' loop doesn't end.

> What can I do?

Forget 'case', try invoking 'semilogy' three times, either as stacked figures ("tall"), or as three separate figures.

>Thanks in advance:)

read 'help' in advance.

Subject: SWITCH expression ERROR !!!!

From: Sadik

Date: 23 Jan, 2013 00:47:09

Message: 6 of 7

Hi anyone,

Please do not let anyone be harsh to a beginner one.

Thank you.

Sadik

Subject: SWITCH expression ERROR !!!!

From: Steven_Lord

Date: 23 Jan, 2013 15:01:57

Message: 7 of 7



"Serhat " <serhat_idd@hotmail.com> wrote in message
news:kdn5ag$3mq$1@newscl01ah.mathworks.com...
> Hi,
>
> I want to plot 3 graphics successively. So I use this code:
>
> for h=1:3
> switch h
> case 1
> semilogy(x, y, 'rx-')
> case 2
> semilogy(x, y, 'bo-')
> case 3
> semilogy(x, y, 'md-')
> end
>
> but it gives this message "" SWITCH expression must be a scalar or string
> constant."
>
> What can I do? Thanks in advance:)

Executing this code on sample x and y data with the following lines to end
the FOR loop:

pause % so I can see each plot
end

ran without any error on my machine. Therefore there's something you're not
showing the group that's contributing or causing this failure.

Set an error breakpoint then run your code.

http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html

When MATLAB stops, take a look at the variables in the workspace and what's
written on the line of code where it stops. If that doesn't indicate to you
the cause of the problem, show the group the line on which MATLAB stopped in
context (ideally simplified down; please don't post 100 lines of code) along
with information about the specific line on which MATLAB stopped.

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

Tags for 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