Retrieve legend handles and text
Show older comments
I want to retrieve legend handles and text of an already created figure.
I am getting the desired output by [~,~,plots,txt] = legend(___).
It works fine on my machine but it doesn't work in an another machine (plots and text return empty).
Also, MATLAB also doesn't recommend this because of graphics issues. So my understanding is that there is graphics problem in the other machine.
So is there another way to get legend handles and text?
3 Comments
Geoff Hayes
on 23 Sep 2016
Praveen - is the same version of MATLAB used on both machines?
Walter Roberson
on 23 Sep 2016
What is it that you need to do with the handles? And which MATLAB releases are involved?
dpb
on 24 Sep 2016
As Geoff and Walter note, revisions are significant here altho I note that the same output syntax is supported from as early as R12 thru current. However, with R2014(or thereabouts) handle graphics changed drastically internally from HG to HG2 and if one plot was generated and saved as .fig file on one system and then displayed on another crossing that great divide it's not too surprising (albeit disappointing) that such low-level compatibility has been broken.
Any hope will depend on these issues and what it is, as Walter asks, you really need to do...likely there are coding changes that can be made to the second system to manage to retrieve the data, but it's possible I suppose that the gulf is just too wide...
Accepted Answer
More Answers (2)
Peter Reinprecht
on 25 Oct 2016
in R2014b or later you can extract the line and legend object separately from an already existing figure
Legend object
hLegend = findobj(gcf, 'Type', 'Legend');
%get text
hLegend.String
Line object
hLines = findobj(gcf, 'Type', 'Line');
2 Comments
Walter Roberson
on 25 Oct 2016
The line objects you get this way are not the line objects belonging to the legend. Even if you
findall(gcf, 'type', 'line')
you will not be able to find the sample line that is used for the legend.
Brandon Ballard
on 7 Sep 2020
If you are looking for a way to adjust the length of the lines in an existing legend then I would suggest using the following commands.
hLegend = findobj(gcf, 'Type', 'Legend');
hLegend.ItemTokenSize = [x1,x2];
By default the values for x1 and x2 are 30 and 18 respectively, so reducing the numbers will result in a smaller line in the legend window. However, the line style will remain identical to the lines used in the plot.
This code is only an application of that found in the link below:
Captain Karnage
on 22 Dec 2022
Edited: Captain Karnage
on 22 Dec 2022
I'll admit that findobj is a quicker, easier way to find the legends, but in case you want a way that kind of shows you the structure and where the legend is at, this works in R2022b (don't know about previous versions):
legendclass = 'matlab.graphics.illustration.Legend';
%Get the currentfigure
thisFigure = gcf;
%Get figure children
theseChildren = thisFigure.Children; %Alternately, gcf().Children works if you don't need a handle to the figure object
numChildren = numel(theseChildren);
for child = 1:numChildren
thischild = theseChildren(child);
if ~isa( thischild, legendclass )
thisLegend = thischild;
end
end
If you might have multiple legends, you can make thisLegend a cell array and increment an index counter each time a legend is found.
The one advantage here is you can get handles to the various objects in the structure along the way if you need them for other operations.
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!