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:
Plotting within a loop question...

Subject: Plotting within a loop question...

From: cabrego

Date: 17 Mar, 2009 13:35:03

Message: 1 of 4

Hi all,

I am automatically processing data for analysis. Within my loop I am importing data varibales from files and manipulating for plotting. My question is about the actual plotting process and how to automatically change the color / legend of the plots. This is the basic structure of my loop:

for i:n
r(i).id=cfile; %file name
r(i).X=importdata(cfile) % import
r(i).Y=importdata(cfile)
XX=reshape(r(i).X);%manipulation
YY=reshape(r(i).Y);
stairs(XX,YY) %plot
%legend(cfile)
hold on
end

This works pretty well for plotting all of the data on the same plot with the same color line. I would like to change the color of each line with in the loop, and give each line a name in the legend defined by the string cfile.

Subject: Plotting within a loop question...

From: Srikanth

Date: 17 Mar, 2009 15:49:32

Message: 2 of 4


> This works pretty well for plotting all of the data on the same plot with=
 the same color line. =A0I would like to change the color of each line with=
 in the loop, and give each >line a name in the legend defined by the strin=
g cfile.

One thing you could try would be to make a legend with all the
filenames - matlab ignores the extra legend entries, so only those you
have plotted so far should show up on the legend.
As for changing colors, try to create a color-vector (see jet/winter/
bone, for example). Then, when you plot the figure, use color_vector
(i,:).
(Also, as a side note, it is in general better not to use i or j as
variable names - they are also used as the square root of -1).

If your Y axis is always going to remain the same, and in case you
aren't really interested in showing each line being plotted
individually (i.e. how your plot varies as time progresses), you might
store all the data first into a 2-D matrix and then plot it at one
time - the colors are automatically assigned (it works for plot, not
sure about stairs).

hth

Subject: Plotting within a loop question...

From: cabrego

Date: 17 Mar, 2009 16:32:01

Message: 3 of 4

Srikanth <skt@xdtech.com> wrote in message <3769339f-615f-4d6d-af65-87b31ddc821d@i20g2000prf.googlegroups.com>...
>
> > This works pretty well for plotting all of the data on the same plot with=
> the same color line. =A0I would like to change the color of each line with=
> in the loop, and give each >line a name in the legend defined by the strin=
> g cfile.
>
> One thing you could try would be to make a legend with all the
> filenames - matlab ignores the extra legend entries, so only those you
> have plotted so far should show up on the legend.
> As for changing colors, try to create a color-vector (see jet/winter/
> bone, for example). Then, when you plot the figure, use color_vector
> (i,:).
> (Also, as a side note, it is in general better not to use i or j as
> variable names - they are also used as the square root of -1).
>
> If your Y axis is always going to remain the same, and in case you
> aren't really interested in showing each line being plotted
> individually (i.e. how your plot varies as time progresses), you might
> store all the data first into a 2-D matrix and then plot it at one
> time - the colors are automatically assigned (it works for plot, not
> sure about stairs).
>
> hth

Thanks for the input. In general your suggestions are valid and will work but i would like to avoid doing this operations (legend entries, plot entries) manually because I will be plotting quite a few curves. Thanks,

Subject: Plotting within a loop question...

From: Srikanth

Date: 17 Mar, 2009 16:59:36

Message: 4 of 4


> Thanks for the input. =A0In general your suggestions are valid and will w=
ork but i would like to avoid doing this operations (legend entries, plot e=
ntries) manually because >I will be plotting quite a few curves. =A0Thanks,

You don't need to do it manually - you are already pulling out the
legend name as cfile. Instead of just using cfile, use a vector legvec
(i)=3Dcfile - this way, the new name just adds on to the list of
filenames.

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