Linkaxes causes change in current figure based on figure visibility. How to link the x axes without current figure changing?

2 visualizaciones (últimos 30 días)
I have numerous figures that are invisible. For debugging purposes, I am turning one visible at a time to ensure that they look as I expect. I noticed that the call to linkaxes is changing my current figure from whatever I have it set to to the figure that is visible. So once I make the call to linkaxes, any formatting (title, labels, background color, etc...) that is following that call is being applied to the visible figure rather than to the figure that I expect.
Here is a MWE that shows the problem:
close all
clearvars
%pre-allocate graphics objects
figs = gobjects(2,1);
%make figures with visibility off
for i1 = 1:length(figs)
figs(i1)=figure('visible','off');
end
%create cell for axes handles
ax=cell(2,1);
%pre-allocate graphics objects for axes handles
ax{1}=gobjects(2,1);
ax{2}=gobjects(2,1);
%% Plot on Visible Figure 2
%set current figure to figure 2
set(0,'CurrentFigure',figs(2))
%set figure 2 visibility to on
set(figs(2),'visible','on')
%create upper axes in figure 2
ax{2}(1) = subplot(2,1,1,'Xscale','log');
%plot on upper axes in figure 2
plot(1:10);
%create lower axes
ax{2}(2) = subplot(2,1,2,'Xscale','log');
%plot on lower axes in figure 2
plot(1:10);
%% Plot on Invisible Figure 1
%set current figure to figure 1
set(0,'CurrentFigure',figs(1))
%set figure 1 visibility to on
% set(figs(1),'visible','on')
%create upper axes in figure 1
ax{1}(1) = subplot(2,1,1,'Xscale','log');
%plot on upper axes in figure 1
plot(1:10);
%create lower axes in figure 1
ax{1}(2) = subplot(2,1,2,'Xscale','log');
%plot on lower axes in figure 1
plot(1:10);
%% Linkaxes Changes Current Figure Based on Figure (1) Visibility
%check current figure - expecting and get Figure (1)
get(0, 'CurrentFigure')
%link x axes for axes associated with Figure (1)
linkaxes(ax{1},'x')
%check current figure - expecting Figure (1) and get Figure (2) if figs(1)
%visibility is off - if figs(1) visibility is set to on from the commented
%line, then the current figure is Figure(1) as expected
get(0, 'CurrentFigure')
%adding title appears on Figure (1) or Figure (2) depending on the
%visibility of Figure (1)
title('hi')
%turn on visibility of all figures
set(figs,'visible','on')
Additionally, I noticed that if I change the visibility of figs(1) to on after setting it to my current figure, then the problem goes away.

Respuesta aceptada

Jan
Jan el 17 de En. de 2019
Instead of setting the current figure by:
set(0,'CurrentFigure',figs(2))
and relying on the hope, that the current fuigure is not changed (e.g. when a user clicks on it unexpectedly), it is much safer to define the 'Parent' property whenever a graphical element is created:
ax{2}(1) = subplot(2,1,1,'Xscale','log', 'Parent', figs(2));
plot(ax{2}(1), 1:10);
% Or: plot(1:10, 'Parent', ax{2}(1))
Then it does not disturb anything, if the CurrentFigure is changed.
It is some work to include this in general, but I've seen too many GUIs fail due to users clicking on objects pre-maturely or at unexpected times. Callbacks of timers can change the current figure and axes also.
This is the same problem as relying on the current directory to be static, although any callback can change it. Therefore using absolute path names is required for a stable program.
  1 comentario
Shawn Treacy
Shawn Treacy el 17 de En. de 2019
Thanks for the recommendation. It is surely some work, but it absolutely looks like the right thing to do. It looks like this concept can be extended through all formatting calls to xlabel, title, figure color, etc..., as well, which would solve all of the issues that I am having.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by