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

1 次查看(过去 30 天)
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 个评论
Geoff Hayes
Geoff Hayes 2015-10-17
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 2015-10-20
编辑:Jose Peñaloza 2015-10-20
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

请先登录,再进行评论。

回答(1 个)

Stalin Samuel
Stalin Samuel 2015-10-17
%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 个评论
Jose Peñaloza
Jose Peñaloza 2015-10-20
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by