Borrar filtros
Borrar filtros

How could I use Legend function in a for loop with an array?

1 visualización (últimos 30 días)
Jose Peñaloza
Jose Peñaloza el 15 de Oct. de 2015
Comentada: Jose Peñaloza el 20 de Oct. de 2015
Hi! everyone I am trying to insert a Legend in a plot with different names but in a sequence. The problem basically is when I plot Legend, it only show me the first or the last filename I require. So I hope you can help me because I tried with different methodologies and it doesn't work. My code is the next:
for k=1:numfiles
figure(2)
legend(filename(k));
plot3(f{1,k}'/1000,((k*10^2)*10^6)+Ejey{1,k},2*q_magC{1,k},(colour(k)))
title('TRANSFORMADA DE FOURIER')
xlabel(' Frecuencia (kHz) ')
zlabel(' Magnitud ')
view(40,35)
grid on
hold all
end
Thanks in advance!
  2 comentarios
Geoff Hayes
Geoff Hayes el 17 de Oct. de 2015
Jose - if I do something similar and create the legend outside of the loop (or even as you have shown above within at each iteration of the loop), then all of the filenames appear as expected as legend entries. Can you show us what the filename variable is initialized to? How many curves are being drawn on your figure?
Jose Peñaloza
Jose Peñaloza el 20 de Oct. de 2015
Editada: Jose Peñaloza el 20 de Oct. de 2015
Thank you! finally I did it, the code that let me:
for k=1:numfiles
figure(2)
leyenda=filename(1:k)
legend(leyenda)
plot3(f{1,k}'/1000,((k*10^2)*10^6)+Ejey{1,k},2*q_mag{1,k},(colour(k)))
xlim ([0 25])
title('TRANSFORMADA DE FOURIER')
xlabel(' Frecuencia (kHz) ')
zlabel(' Magnitud ')
view(40,35)
grid on
hold on
end

Iniciar sesión para comentar.

Respuestas (1)

Stalin Samuel
Stalin Samuel el 17 de Oct. de 2015
%try like this
s = {'L1','L2','L3'};
for i = 1:length(s)
plot(rand(10,1),'color',rand(1,3));
hold on
end
legend(s)
  1 comentario
Jose Peñaloza
Jose Peñaloza el 20 de Oct. de 2015
Thank you for your help! Finally I did it with that code
for k=1:numfiles
figure(2)
leyenda=filename(1:k)
legend(leyenda)
plot3(f{1,k}'/1000,((k*10^2)*10^6)+Ejey{1,k},2*q_mag{1,k},(colour(k)))
xlim ([0 25])
title('TRANSFORMADA DE FOURIER')
xlabel(' Frecuencia (kHz) ')
zlabel(' Magnitud ')
view(40,35)
grid on
hold on
end

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by