saastn
saastn

Reputation: 6015

How to make an axes current which is inside a figure created by GUIDE?

I created a bank figure using GUIDE and put an axes object inside it and saved the figure. Now I want to load the figure and set its axes as current axes object. This is my code:

close all; clear all; clc;
fh = openfig('test.fig');
ah = findobj(fh, 'tag', 'axes1');
figure(fh);
axes(ah);
plot(rand(10, 1));

But plot creates a new figure and plots in it! Am I missing something?

I know that I can solve it with plot(ah, ...), but I want to make gca to return this new axes. I have a lot of plotting codes that I want to be drawn in this new axes.

Upvotes: 1

Views: 344

Answers (1)

Suever
Suever

Reputation: 65460

By default, HandleVisibility of GUIDE figures is set such that they aren't automatically detected. For example if you load the figure and then call gcf, you'll also create a new figure.

To have the plot be placed within the axes, you can specify the axes explicitly as the parent of the plot command.

plot(rand(10, 1), 'Parent', ah)

Alternately, you could specify that the HandleVisibility of the figure is 'on'. And then plot will be able to find it. This could be done by either setting the value of HandleVisibility using the property editor in GUIDE or calling the set function:

set(fh, 'HandleVisibility', 'on')

I recommend the first option as explicitly specifying the parent axes is always better than implicit.

Upvotes: 1

Related Questions