From: sdv on
I would like to append lines in the legend for every plot cycle in the following loop. The plotting takes place inside a function, which gets A=[] as an argument. Here, I am emulating this with a simple for loop. The length(A) is an arbitrary number, most likely to vary. Can anyone help? Thanks in advance, sdv

for k = 1:1:length(A)
figure(1)
plot(X(k), Y(k));
legend(num2str(A(k)), 'Location', 'Best');
hold all;
end
From: S. Kauth on
Am 09.03.2010 12:39, schrieb sdv:
> I would like to append lines in the legend for every plot cycle in the
> following loop. The plotting takes place inside a function, which gets
> A=[] as an argument. Here, I am emulating this with a simple for loop.
> The length(A) is an arbitrary number, most likely to vary. Can anyone
> help? Thanks in advance, sdv
>
> for k = 1:1:length(A)
> figure(1)
> plot(X(k), Y(k));
> legend(num2str(A(k)), 'Location', 'Best');
> hold all;
> end

I once accomplished this by something like this:

figure(1);
legendtext ='';
for k = 1:1:length(A)
plot(X(k), Y(k));
legendtext = [legendtext; num2str(A(k)), 'Location', 'Best']
hold all;
end
legend(legendtext);
From: Husam Aldahiyat on
"sdv " <sofia.vatti(a)m4s.be> wrote in message <hn5c0n$i2s$1(a)fred.mathworks.com>...
> I would like to append lines in the legend for every plot cycle in the following loop. The plotting takes place inside a function, which gets A=[] as an argument. Here, I am emulating this with a simple for loop. The length(A) is an arbitrary number, most likely to vary. Can anyone help? Thanks in advance, sdv
>
> for k = 1:1:length(A)
> figure(1)
> plot(X(k), Y(k));
> legend(num2str(A(k)), 'Location', 'Best');
> hold all;
> end

Use findobj() and the handle syntax of using legend(), both outside the loop.