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 allclearvars%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.
Best Answer