find line handles related to legend entries
Show older comments
Before R15 I used to be able to find the handle of an object, referred to in the legend of an existing graph by a certain legend string, say "measured" as follows:
hlegend=findobj(gcf,'Type','axes','Tag','legend');
legenddata=get(hlegend,'userdata');
legendstrings=legenddata.lstrings;
ii=find(strcmp('measured', legendstrings));
h_measured=legenddata.handles(ii);
I dont see how to do something similar with the new graphics handles system in R15. Any suggestions?
Accepted Answer
More Answers (1)
Brendan Hamm
on 7 Jul 2015
The legend will no longer store this information unless you pass it to the UserData. I would suggest returning the objects created by your various graphics commands anytime you will need to access the data later.
f = figure;
ax = axes;
p = plot(randn(100,5)); % Make a plot of some random data
leg = legend('A','B','C','D','E'); % Create legend and return the object
leg.UserData = p; % We could assign the handles to the legend directly
idx = strcmp(leg.String,'B')
B = leg.UserData(idx) % returns the Line object
% We don't really need to store to the UserData.
Balt = p(idx); % Alternative without storage.
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!