Info

This question is closed. Reopen it to edit or answer.

I have problems with multi legends

1 view (last 30 days)
Korhan Cengiz
Korhan Cengiz on 11 Nov 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
Can you give some idea for my problem like this:
for plot legend end
then
if (main)
if 1
plot
legend
if 2
plot
legend
...
if n
plot
legend
end
end
for each legend I want to renew the legend box and I want to add it to box and I want to update and enlarge the box.
Thanks

Answers (1)

dpb
dpb on 11 Nov 2015
Edited: dpb on 11 Nov 2015
First, save the handle to the legend on creation. Then, also save the line handles created by plot each time in an array and augment the handle 'String' property associating the the cell array of titles with the line plot handles...
hL=zeros(N,1); % for line handles
hL(1)=plot(...; % first line
lab{1,1}=num2str(1,'Line %2d'); % first label
hLg=legend(lab); % create the legend object, first label
hold on % to add onto existing...
for i=2:N % subsequent after first preparation
hL(i)=plot(...; % next line
lab{i,1}=num2str(i,'Line %2d'); % and next label add to cell array
legend(hL,lab); % apply the current labels to the existing line handles..
end
Alternatively, of course, you can build the array of data as a 2D array and wait and plot all in "one swell foop" and then legend can automagically do the association of an array of labels one:one with the lines (columns) in the array. But, the above is the pattern to do it a line at a time which has its reasons for doing so, granted...
  2 Comments
Korhan Cengiz
Korhan Cengiz on 11 Nov 2015
thanks your reply but can you send me example with usage of if clauses.
dpb
dpb on 11 Nov 2015
What about if? There are examples at
doc if
The issue you outlined above re: adding to a legend has to do with keeping track of the handles of the objects in the axes to be added to an existing legend, not if...that appears a distraction.

Tags

Community Treasure Hunt

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

Start Hunting!