retrieve legend icon property from existing legend

16 views (last 30 days)
hi all,
in the documentation to legend (R2014b) it says
[h,icons,plots,str] = legend(___) %additionally returns the objects used to create the legend icons, the objects plotted in the graph, and an array of text strings.
for a new legend the icons object can be changed like
icon(i).MarkerSize=25;
if you know wich i you have to refer to. i want to alter the appearance of legend markers in an existing figure that i saved in a fig file. i do not want to plot it all anew. is there any more elegant way to retrieve an existing icons object than this one:
l=findall(gcf,'tag','legend');
[lh,ic,~,~]=legend(l.String,'Interpreter',l.Interpreter,'FontSize',l.FontSize,...
'TextColor',l.TextColor,'Units',l.Units,'UserData',l.UserData,'FontWeight',l.FontWeight,...
'Position',l.Position,'Color',l.Color,'Box',l.Box,'FontName',l.FontName,...
'Location',l.Location,'LineWidth',l.LineWidth); % transfer all properties from existing legend
delete(l)
i=findall(ic,'Type','Line'); % reduce to lines
for m=1:length(i) % change properties
i(m).MarkerSize=25;
end
thanks, Fabian
  2 Comments
Fabian
Fabian on 20 May 2015
i dont know why this did not work in yesterday's Matlab session (i tried that, sure), but the solution worked today and is as simple as can be:
[lh,ic]=legend;
with ic the legend icon object where one can then find the line objects as posted above
Fabian
Fabian on 18 Jun 2015
well using my function again on a plotyy-generated graph i was unable to return the icons in any way.
legend(axis) % with axis handle to where the legend is set
the lh was returned, but ic was empty...

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!