# How can I keep figures invisible when going between different figures and different subplots?

5 views (last 30 days)
Jennifer Davies on 26 Oct 2016
Commented: Jennifer Davies on 28 Mar 2017
Hi I have a script with three figures (f1, f2, and f3) that are invisible to the user, each with four subplots (sp1, sp2, sp3 and sp4). I need to call these figures at various times in the script to plot data. E.g., I first plot on f1-sp1, then on f2-sp1, and f3-sp1. In the next iteration of the loop I plot on f1=sp2, f2-sp2, f3-sp2, etc... Is anyone please able to tell me how to do this without making the figures visible? For each figure I can assign handles to the subplots with the following code,
for sp=1:4
ax(sp)=subplot(2,2,sp);
end
and then with the current figure I can move around the subplots using
subplot(ax(2))
I can do this for each figure, i.e., end up with ax1, ax2 and ax3, but using these in the codes only seems to move within the current figure. E.g.,
subplot(ax2(4))
will make subplot 4 of the current figure active, but will not necessarily make subplot 4 of figure 2 active. If I make figure 2 current using
figure(2)
then it becomes visible.
Can anyone help me with how to make a specific subplot of a specific figure current, without making the figure visible?
Thanks very much Jen

Thorsten on 26 Oct 2016
Don't use
figure(2)
but
set(0, 'CurrentFigure', f(2))
where f(2) is the handle of figure 2, obtained with gcf when figure 2 is active, or returned by figure when the figure is created.
Jennifer Davies on 26 Oct 2016
Thank you!

hFigs(1) = figure(1);
for sp=1:4
ax(sp)=subplot(2,2,sp, 'Parent', hFigs(1));
end
hFigs(2) = figure(2);
for sp=1:4
ax(sp+4)=subplot(2,2,sp, 'Parent', hFigs(2));
end
Something like that would work better. I wouldn't store my axes indexed quite like that probably because a hard-coded +4 is ugly and it depends where your code is fitting with respect to each other as to exactly how I would do it, but the main point is to explicitly give your subplot axes a parent figure.
Relying on making something the current figure and then using the default subplot instruction is never a good idea.
Jennifer Davies on 26 Oct 2016
Ok great, I will try this now. Thank you
Jennifer Davies on 28 Mar 2017
Hi Adam I'm trying again with this code and wondered if you're able to see where I'm going wrong? I'm creating two figures, each with six subplots, using the following code:
for fig=1:2
figure('papertype','A4','paperorientation','portrait','Visible','Off')
for sp=1:6
ax{fig,sp}=subplot(3,2,sp,'parent',fig);
end
end
I can then toggle between the subplots using axes, e.g., to get to subplot 4 on figure 2 I use
axes(ax{2,4})
But this makes the figure visible. Do you know if there's a way to keep it invisible?