필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Jose Peñaloza
Jose Peñaloza 2015년 10월 15일
댓글: Jose Peñaloza 2015년 10월 20일
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