Call to Subplot is Adding Axis to Current Figure

1 vue (au cours des 30 derniers jours)
Shawn Treacy
Shawn Treacy le 5 Avr 2019
Commenté : Shawn Treacy le 10 Avr 2019
I'm creating a figure and then later adding a subplot to it. When I get the axis handle from the call to subplot, it is adding an axis to what I think is the current figure.
%pre-allocate graphics objects for holding figures
figs = gobjects(2,1);
%create figures and store in variable figs
for i1 = 1:length(figs)
figs(i1) = figure;
end
%create the upper axis of a 2x1 subplot and get the handle
%this call is also adding an axis to figs(2) for some reason
ax = subplot(2,1,1,'Parent',figs(1))
If I add a call to
set(0,'CurrentFigure',figs(1))
just before
ax = subplot(2,1,1,'Parent',figs(1))
then it won't create an axis on the second figure. Can anyone explain this phenomenon? Is there a better solution than my workaround of having to set the current figure before any of these calls?
  4 commentaires
A. Sawas
A. Sawas le 6 Avr 2019
I did not get this problem ... I am using 2018b
Shawn Treacy
Shawn Treacy le 10 Avr 2019
That's strange. I don't have 2018b to test, but I checked it on 2018a and 2019a and have the same issue.
I wonder if it could be related to some startup code that I added to get the zoom/pan/etc... functions back on the toolbar.
set(groot,'defaultFigureCreateFcn','addToolbarExplorationButtons(gcf)')
set(groot,'defaultAxesCreateFcn','set(get(gca,''Toolbar''),''Visible'',''off'')')
I'm wondering if that second call is the problem.

Connectez-vous pour commenter.

Réponse acceptée

Adam
Adam le 10 Avr 2019
Yes, that does seem a slightly odd thing to have in the startup file and is indeed the cause of this behaviour (I just tested with and without it).
I have the first line in my startup file also, but not the second line.
Try replacing that line with the following in your startup instead:
set(groot,'defaultAxesCreateFcn',@(ax,~)set(ax.Toolbar,'Visible','off'))
  3 commentaires
Shawn Treacy
Shawn Treacy le 10 Avr 2019
That's great. It addresses the problems that come from uses gcf and gca. The better startup commands seem to be:
set(groot,'defaultFigureCreateFcn',@(fig,~)addToolbarExplorationButtons(fig))
set(groot,'defaultAxesCreateFcn',@(ax,~)set(ax.Toolbar,'Visible','off'))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by