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

6 views (last 30 days)
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.

Accepted Answer

Matt J
Matt J on 25 Dec 2012
Edited: Matt J on 25 Dec 2012
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 Comments
Matt J
Matt J on 25 Dec 2012
Edited: Matt J on 25 Dec 2012
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 on 25 Dec 2012
Edited: Matt J on 25 Dec 2012
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 )

Sign in to comment.

More Answers (2)

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 25 Dec 2012
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

Giorgos Papakonstantinou
Giorgos Papakonstantinou on 25 Dec 2012
Thank you Matt

Community Treasure Hunt

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

Start Hunting!