COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Plotting multiple variables generated from a loop in the same graph

• Follow

```I am currently trying to plot a set of datas generated from a loop on the very same graph so I can compare them.

I have set up the loop so that the data is generated like this

Freq (fixed value for X axis)

TL_1 (an array with 125 variables)
TL_2
TL_3
etc...

The amount of TLs generated depends on how the loop is set...so i may vary from 3~30 different sets of value

I tried to do something like this:

for m=1:length(x);
eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
end

but noticed that next plot would overwrite the previous one.

Manually, the command would be entered as such

subplot(311),
plot(Freq,TL_1,Freq,TL_2,Freq,TL_3....)  until the last value
legend('TL_1','TL_2','TL_3'....) until the last value

I believe i still need to use the eval command but can't think it through....Can anyone shed some light on this situation?
```
 0
Reply Nelson 11/15/2010 4:45:05 AM

```On Nov 15, 5:45=A0am, "Nelson Wong" <nelso...@gmail.com> wrote:
> I am currently trying to plot a set of datas generated from a loop on the=
very same graph so I can compare them.
>
> I have set up the loop so that the data is generated like this
>
> Freq (fixed value for X axis)
>
> TL_1 (an array with 125 variables)
> TL_2
> TL_3

Reformat the data so that you have one array of arrays:

TL(1), TL(2), ...

Tt is trivial to handle the data set on this form.

Rune
```
 0
Reply Rune 11/15/2010 4:53:11 AM

```Rune Allnor <allnor@tele.ntnu.no> wrote in message <b74e6263-a612-4ccf-a68f-aa71cf6f3a2f@i17g2000vbq.googlegroups.com>...
> On Nov 15, 5:45 am, "Nelson Wong" <nelso...@gmail.com> wrote:
> > I am currently trying to plot a set of datas generated from a loop on the very same graph so I can compare them.
> >
> > I have set up the loop so that the data is generated like this
> >
> > Freq (fixed value for X axis)
> >
> > TL_1 (an array with 125 variables)
> > TL_2
> > TL_3
>
> Reformat the data so that you have one array of arrays:
>
> TL(1), TL(2), ...
>
> Tt is trivial to handle the data set on this form.
>
> Rune

How should I do this?
If it is a definite set of values, i know i can achieve this by
TLall = [num2cell(TL_1),num2cell(TL_2)...]

but if i generate each array from a loop, how can i set it so that the next set of data is inserted in the next row?

Second of all, if i do this...then when i plot it...do it need to do this as
plot(Freq,TLall(1:125),Freq,TL(126:250)...) ?
```
 0
Reply Nelson 11/15/2010 5:12:03 AM

```On Nov 15, 5:45=A0pm, "Nelson Wong" <nelso...@gmail.com> wrote:
> I am currently trying to plot a set of datas generated from a loop on the=
very same graph so I can compare them.
>
> I have set up the loop so that the data is generated like this
>
> Freq (fixed value for X axis)
>
> TL_1 (an array with 125 variables)
> TL_2
> TL_3
> etc...
>
> The amount of TLs generated depends on how the loop is set...so i may var=
y from 3~30 different sets of value
>
> I tried to do something like this:
>
> for m=3D1:length(x);
> =A0 =A0 eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
> end
>
> but noticed that next plot would overwrite the previous one.
>
> Manually, the command would be entered as such
>
> subplot(311),
> plot(Freq,TL_1,Freq,TL_2,Freq,TL_3....) =A0until the last value
> legend('TL_1','TL_2','TL_3'....) until the last value
>
> I believe i still need to use the eval command but can't think it through=
.....Can anyone shed some light on this situation?

Oh NO!!!
The dreaded eval rears its head again.
This is just the sort of muddle you get yourself into when you use the
evil eval.
DON'T DO IT.

Read Section 4.6 here:
http://matlab.wikia.com/wiki/FAQ

```
 0
Reply TideMan 11/15/2010 7:14:36 AM

```>
> I tried to do something like this:
>
> for m=1:length(x);
>     eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
> end

Add "hold on" command

Bruno
```
 0
Reply Bruno 11/15/2010 7:38:04 AM

```"Nelson Wong" <nelson86@gmail.com> wrote in message <ibqdsg\$3ni\$1@fred.mathworks.com>...
> I am currently trying to plot a set of datas generated from a loop on the very same graph so I can compare them.
>
> I have set up the loop so that the data is generated like this
>
> Freq (fixed value for X axis)
>
> TL_1 (an array with 125 variables)
> TL_2
> TL_3
> etc...
>
> The amount of TLs generated depends on how the loop is set...so i may vary from 3~30 different sets of value
>
> I tried to do something like this:
>
> for m=1:length(x);
>     eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
> end
>
> but noticed that next plot would overwrite the previous one.
>
> Manually, the command would be entered as such
>
> subplot(311),
> plot(Freq,TL_1,Freq,TL_2,Freq,TL_3....)  until the last value
> legend('TL_1','TL_2','TL_3'....) until the last value
>
> I believe i still need to use the eval command but can't think it through....Can anyone shed some light on this situation?

Hav eu tried putting 'hold on' after eval command.
all d best
```
 0
Reply nagaraju 11/15/2010 7:56:03 AM

```The hold on command will help you, but in principle I think that all the plots you make will have the same color... unless you define something like an array of colors before.

Wjy don't you add each variable you are generating as a new row of a matrix that will be growing inside a loop? (if you know previously the size it would be better to preallocating it to the sizes with Tt=zeros(number_of_rows,number_of_columns) but I guess this is not the case). One you have this matrix you can plot outside the loop with:
subplot(311)
plot(Freq,TL_t)

For the legend you can define it in a loop:
Legend=cell(1,size(TL_t,1));
for k=1:size(TL_t,1)
Legend{k} = ['TL_ ',num2str(k)];
end

Hope it helps,
Ana

"Nelson Wong" <nelson86@gmail.com> wrote in message <ibqdsg\$3ni\$1@fred.mathworks.com>...
> I am currently trying to plot a set of datas generated from a loop on the very same graph so I can compare them.
>
> I have set up the loop so that the data is generated like this
>
> Freq (fixed value for X axis)
>
> TL_1 (an array with 125 variables)
> TL_2
> TL_3
> etc...
>
> The amount of TLs generated depends on how the loop is set...so i may vary from 3~30 different sets of value
>
> I tried to do something like this:
>
> for m=1:length(x);
>     eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
> end
>
> but noticed that next plot would overwrite the previous one.
>
> Manually, the command would be entered as such
>
> subplot(311),
> plot(Freq,TL_1,Freq,TL_2,Freq,TL_3....)  until the last value
> legend('TL_1','TL_2','TL_3'....) until the last value
>
> I believe i still need to use the eval command but can't think it through....Can anyone shed some light on this situation?
```
 0
Reply Ana 11/15/2010 8:15:05 AM

```People, thank you for the replies!

Let me update my status right here...

Hold on works but as Ana has pointed out, all the plots are in the same color and it is not feasible for my purpose at the moment.

I actually did the job with a bunch of sprintfs and strcat but the code is very unclean and i don't like it.

I'm trying to define the variables as a matrix and plot it out.

Working on it right now...

"Ana " <anita.gg@gmail.com> wrote in message <ibqq69\$bqp\$1@fred.mathworks.com>...
> The hold on command will help you, but in principle I think that all the plots you make will have the same color... unless you define something like an array of colors before.
>
> Wjy don't you add each variable you are generating as a new row of a matrix that will be growing inside a loop? (if you know previously the size it would be better to preallocating it to the sizes with Tt=zeros(number_of_rows,number_of_columns) but I guess this is not the case). One you have this matrix you can plot outside the loop with:
> subplot(311)
> plot(Freq,TL_t)
>
> For the legend you can define it in a loop:
> Legend=cell(1,size(TL_t,1));
> for k=1:size(TL_t,1)
>     Legend{k} = ['TL_ ',num2str(k)];
> end
>
> Hope it helps,
> Ana
>
>
> "Nelson Wong" <nelson86@gmail.com> wrote in message <ibqdsg\$3ni\$1@fred.mathworks.com>...
> > I am currently trying to plot a set of datas generated from a loop on the very same graph so I can compare them.
> >
> > I have set up the loop so that the data is generated like this
> >
> > Freq (fixed value for X axis)
> >
> > TL_1 (an array with 125 variables)
> > TL_2
> > TL_3
> > etc...
> >
> > The amount of TLs generated depends on how the loop is set...so i may vary from 3~30 different sets of value
> >
> > I tried to do something like this:
> >
> > for m=1:length(x);
> >     eval(['subplot(311), plot(Freq,TL_',num2str(m),')']);
> > end
> >
> > but noticed that next plot would overwrite the previous one.
> >
> > Manually, the command would be entered as such
> >
> > subplot(311),
> > plot(Freq,TL_1,Freq,TL_2,Freq,TL_3....)  until the last value
> > legend('TL_1','TL_2','TL_3'....) until the last value
> >
> > I believe i still need to use the eval command but can't think it through....Can anyone shed some light on this situation?
```
 0
Reply Nelson 11/15/2010 8:39:04 AM

```Thanks everybody

I have adopted the two dimensional array approach, and the code is very clean.

Thanks again for all the excellent advices.
```
 0
Reply Nelson 11/15/2010 9:45:21 AM

```"Nelson Wong" <nelson86@gmail.com> wrote in message <ibqrj8\$bje\$1@fred.mathworks.com>...
> People, thank you for the replies!
>
>
> Hold on works but as Ana has pointed out, all the plots are in the same color and it is not feasible for my purpose at the moment.
>

A hint: If you need to plot different colors in separate plot commands, use
hold all
That allows to cycle through the colors in colororder.

But in this case the basic problem was making those variables in a loop. Nobody should ever do that as explained in FAQ 4.6.
```
 0
Reply Pekka 11/15/2010 10:44:04 AM

9 Replies
297 Views

(page loaded in 0.07 seconds)

Similiar Articles:

7/22/2012 4:26:06 AM