I would like to find the handle to one legend that appears in a specific subplot, while there are a lot of other plots with legends open. I know how to get a handle to all the legends, but not how to find the handle to one specific legend. Here is an example,
%% Make example plot with several subplots and legends
f1 = figure;nSubplot = 3;for iPlot = 1:nSubplot ax(iPlot) = subplot(nPlot,1,iPlot); % Make an axis that will have its own legend
h1 = plot(rand(10,1), rand(10,1), '*'); % Plot some data
hold on; h2 = plot(rand(10,1), rand(10,1), 'o'); box off; hIgnore(iPlot) = legend([h1 h2], 'a', 'b'); % Make a legend in this subplot
% I know in this example I could create the legend handle hIgnore when I make the legend,
% but please ignore this approach since in the real code I must find the existing handle
end%% Now try to get handle to legend from first subplot
hLeg = findobj('tag','legend'); % Returns handles to *all* legends, which we don't want
% Try to get handle to legend in first subplot only
hLeg = findobj(ax(1), 'tag','legend'); % Returns nothing, even though ax(1) is handle to first subplot
Best Answer