Michael
Michael

Reputation: 1009

How to find axes by YLabel string?

I am creating a figure with multiple subplots and saving it to a file. like this:

fig = figure;
ax1 = subplot(2, 1, 1);
ax2 = subplot(2, 1, 2);
ylabel(ax1, 'First');
ylabel(ax2, 'Second');
savefig('myfigure.fig')

Later, I want to copy one of the subplots to a new figure without re-running the code that creates the figure. My current approach is to load the saved figure, locate the axes I want to copy by its YLabel, and then copying it to a new figure:

newfig = figure;
oldfig = openfig('myfigure.fig');
ylabel_obj = findobj(oldfig, 'String', 'First');  % This is not givng me what I expect
old_axes_obj = ylabel_obj.Parent;
new_axes_obj = copyobj(old_axes_obj, newfig);

The problem is that findobj above is not finding the YLabel. It just returns a 0x0 empty GraphicsPlaceholder array. Why isn't findobj finding my YLabel? Is there a better way to find the axes I want?

Upvotes: 4

Views: 146

Answers (1)

gnovice
gnovice

Reputation: 125874

The handle visibility for the label text object is turned off, so it will not show up in the Children property of its parent axes, and therefore won't be found when using findobj. You can instead use findall to get around this limitation:

ylabel_obj = findall(oldfig, 'String', 'First');

Alternatively, you can set the root ShowHiddenHandles property to 'on' to list all object handles regardless of their HandleVisibility property setting, making findobj and findall equivalent.

Upvotes: 6

Related Questions