Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 17 Oct 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 on 20 Oct 2015
Edited: Jose Peñaloza on 20 Oct 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

Sign in to comment.

Answers (1)

Stalin Samuel
Stalin Samuel on 17 Oct 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 Comment
Jose Peñaloza
Jose Peñaloza on 20 Oct 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

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!