Thread Subject:
plotting in a for loop

Subject: plotting in a for loop

From: Jeffrey

Date: 21 Jul, 2012 18:43:14

Message: 1 of 8

Hi,

I have many vectors that I want to plot in a for loop (x1 x2 x3 x4 y1 y2 y3 y4). How do you assign the index in the for loop to the vector you want to plot?

example
i=1:1:4

plot(xj,yj)?????

Thanks,

Subject: plotting in a for loop

From: dpb

Date: 21 Jul, 2012 18:56:50

Message: 2 of 8

On 7/21/2012 1:43 PM, Jeffrey wrote:
> Hi,
>
> I have many vectors that I want to plot in a for loop (x1 x2 x3 x4 y1 y2
> y3 y4). How do you assign the index in the for loop to the vector you
> want to plot?
>
> example
> i=1:1:4
>
> plot(xj,yj)?????

Where's the data?

If they're in the array x,y where x is the same for each y then

plot(x,y)

will do all in "one swell foop"

If you really mean you have variables named x1...xN, etc., the answer is
"don't do that" -- use arrays or structures instead.

FAQ 4.6

<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

--

Subject: plotting in a for loop

From: Jeffrey

Date: 21 Jul, 2012 19:26:22

Message: 3 of 8



I am trying to plot each data set separately in a for loop to make a animations. There are hundreds of plots.

Maybe a better question is:
 If I have vectors x1, x2,..., xn y1, y2,..., yn. I want to plot (x1,y1) then update the figure with plot (x2, y2) and so on.
 Maybe there are better ways to do this but how can I write x1,..., xn in a for loop where i=1:n.
 I try something like ['x' num2str(i)] but this does not display the vector xi?

Thanks,

 





dpb <none@non.net> wrote in message <jueu1g$bc5$1@speranza.aioe.org>...
> On 7/21/2012 1:43 PM, Jeffrey wrote:
> > Hi,
> >
> > I have many vectors that I want to plot in a for loop (x1 x2 x3 x4 y1 y2
> > y3 y4). How do you assign the index in the for loop to the vector you
> > want to plot?
> >
> > example
> > i=1:1:4
> >
> > plot(xj,yj)?????
>
> Where's the data?
>
> If they're in the array x,y where x is the same for each y then
>
> plot(x,y)
>
> will do all in "one swell foop"
>
> If you really mean you have variables named x1...xN, etc., the answer is
> "don't do that" -- use arrays or structures instead.
>
> FAQ 4.6
>
> <http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>
>
> --

Subject: plotting in a for loop

From: dpb

Date: 21 Jul, 2012 19:55:25

Message: 4 of 8

On 7/21/2012 2:26 PM, Jeffrey wrote:
...

> Maybe there are better ways to do this but how can I write x1,..., xn in
> a for loop where i=1:n.
> I try something like ['x' num2str(i)] but this does not display the
> vector xi?
...

>> FAQ 4.6
>>
>>

<http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>


Yes, there _is_ a better way...don't create the multiple variables but
use an array and then if you really want the plots individually in a loop

plot(x(:,1), y(:,1);
hold on
for i=1:size(x,2)
   plot(x(:,i), y(:,i);
end

If for some unstated reason you can't use an array, "have your cake and
eat it too" -- used structures w/ dynamic field names. Also addressed
in the above FAQ

--

Subject: plotting in a for loop

From: Jeffrey

Date: 22 Jul, 2012 02:46:34

Message: 5 of 8


I would like to put the vectors in an array but this is part of my question, I don't know how to call out the vectors labeled x1,x2,x3,...xn. I apologize for such a basic question and I know the answer is very simple.

what if I wanted to display all the vectors without typing all of them

i=1:1:n
x what goes here?
end




dpb <none@non.net> wrote in message <juf1fb$jj9$1@speranza.aioe.org>...
> On 7/21/2012 2:26 PM, Jeffrey wrote:
> ...
>
> > Maybe there are better ways to do this but how can I write x1,..., xn in
> > a for loop where i=1:n.
> > I try something like ['x' num2str(i)] but this does not display the
> > vector xi?
> ...
>
> >> FAQ 4.6
> >>
> >>
>
> <http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>
>
>
> Yes, there _is_ a better way...don't create the multiple variables but
> use an array and then if you really want the plots individually in a loop
>
> plot(x(:,1), y(:,1);
> hold on
> for i=1:size(x,2)
> plot(x(:,i), y(:,i);
> end
>
> If for some unstated reason you can't use an array, "have your cake and
> eat it too" -- used structures w/ dynamic field names. Also addressed
> in the above FAQ
>
> --

Subject: plotting in a for loop

From: steve.nospamm@gmail.com

Date: 22 Jul, 2012 03:57:36

Message: 6 of 8

On Saturday, July 21, 2012 1:43:14 PM UTC-5, Jeffrey wrote:
> Hi,
>
> I have many vectors that I want to plot in a for loop (x1 x2 x3 x4 y1 y2 y3 y4). How do you assign the index in the for loop to the vector you want to plot?
>
> example
> i=1:1:4
>
> plot(xj,yj)?????
>
> Thanks,

assign vector use index, like x(j) and y(i). use ii also not i becuase matlab i is the imagine numbers

Subject: plotting in a for loop

From: dpb

Date: 22 Jul, 2012 04:45:09

Message: 7 of 8

On 7/21/2012 9:46 PM, Jeffrey wrote:
>
> I would like to put the vectors in an array but this is part of my
> question, I don't know how to call out the vectors labeled
> x1,x2,x3,...xn. I apologize for such a basic question and I know the
> answer is very simple.
>
> what if I wanted to display all the vectors without typing all of them
>
> i=1:1:n
> x what goes here?
> end
>
...

Let's go back to basics--

Where did these vectors come from originally?

As to the question above, as the FAQ says, to do that means eval() and
that is very rarely the right way to proceed.

So, how did you generate the vectors in the first place and let's get
them created in a more useful form instead...

--

Subject: plotting in a for loop

From: Ioanna

Date: 23 Jul, 2012 11:11:14

Message: 8 of 8

"Jeffrey " <jeffreygsmith@yahoo.com> wrote in message <jufpia$aop$1@newscl01ah.mathworks.com>...
>
> I would like to put the vectors in an array but this is part of my question, I don't know how to call out the vectors labeled x1,x2,x3,...xn. I apologize for such a basic question and I know the answer is very simple.
>
> what if I wanted to display all the vectors without typing all of them
>
> i=1:1:n
> x what goes here?
> end
>
>


You should never ever ever name things x1, x2, x3. In general, you can create a cell array and then call x{1}, x{2} etc.
http://www.mathworks.co.uk/help/techdoc/ref/cell.html

In the specific case where they are arrays of the same size you can add them all to a big array with an extra dimension. For example, if they are vectors of the same length, make a matrix with each vector being a column and call x(:,1), x(:,2) to get data for each vector when you want to plot.

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