Produce a legend in for loop that takes entries from an array

Merry Christmas to all of you. I am trying to incorporate a legend in for loop. my code is the following:
s=['T '; 'Pb '; 'Pf '; 'Pc '; 'Psh '; 'Pito'];
t=cellstr(s);
figure
for k=1:length(pl(end,end,:))
for m=1:length(pl(:,end,end))
subplot(2,3,m) % BUSBAR
semilogx(dim(1,Nmin:Nmax,k)*1000,pl(m,Nmin:Nmax,k),'linewidth',2)
title(t(k))
hold all
end
end
My idea is simple. I want to have a legend that takes values from an array. So if for example I have six curves per plot, I would like to have one legend with 6 entries. I would like something like that because I change very frequently the number of entries and the entries themselves. To give you more details I would like to build a legend with 4 entries and each entry I would like to say 1cm 2cm 3cm 4cm. But I might change that later to 6 different entries.
For that reason I am trying to use a function from the matlab file exchange legend1.m. You can find this in: http://www.mathworks.in/matlabcentral/fileexchange/39505-legend1-m.
I didn't have any success yet. Can you help me please.

 채택된 답변

Matt J
Matt J 2012년 12월 25일
편집: Matt J 2012년 12월 25일
My idea is simple. I want to have a legend that takes values from an array.
That doesn't require any special files. Here's a simplified example,
l={'a','b','c'};
figure
plot(1:5,1:5,1:5,1:5);
legend(l{[2,3]});
figure
plot(1:5,1:5,1:5,1:5);
legend(l{[1,3]});

댓글 수: 2

Matt J
Matt J 2012년 12월 25일
편집: Matt J 2012년 12월 25일
Giorgos commented:
Thank you. Indeed that is much more simple and efficient. On the other hand what if I want to have a constant character cell in all my legend entries and that is cm and add to it the number from an array and this array would be
l=[1 2 3 4]
so the output in the legend would be:
  • 1 cm
  • 2 cm
  • 3 cm
  • 4 cm
Matt J
Matt J 2012년 12월 25일
편집: Matt J 2012년 12월 25일
That has nothing to do with plotting. That's just a problem in how to build a certain cell array of strings:
l=arrayfun(@(i) [num2str(i) ' cm'],1:4,'uni',0 )

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Thank you. Indeed that is much more simple and efficient. On the other hand what if I want to have a constant character cell in all my legend entries and that is cm and add to it the number from an array and this array would be
l=[1 2 3 4]
so the output in the legend would be:
  • 1 cm
  • 2 cm
  • 3 cm
  • 4 cm

카테고리

Community Treasure Hunt

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

Start Hunting!

Translated by