How to set parent figure of subplot which is not the current figure?

94 views (last 30 days)
p
p on 7 May 2014
Commented: p on 9 May 2014
Good evening, I plotted some graphs in several different figures. After that I want to add subfigures to an existing figure which is not the current figure any more. It should be something like this:
hfig1 = figure;
subplot(1, 2, 1);
plot(1, 1);
subplot(1, 2, 2);
plot(1, 1);
hfig2 = figure;
plot(1, 1);
ha1 = findobj(hfig1, 'Type', 'axes');
axis(ha1);
hs = subplot(2, 1, 2); % changed the last number from 1 to 2
spy
Unfortunately, spy is being plotted in the second figure instead of the first one. I do not want to define all subfigures at the beginning. Any suggestions are appreciated ...

Answers (2)

nl2605
nl2605 on 8 May 2014
Well, what you can do is something like this:
hfig1 = figure;
subplot(1, 2, 1,'Tag','axes1');
plot(1, 1);
subplot(1, 2, 2,'Tag','axes2');
plot(1, 1);
hfig2 = figure;
plot(1, 1);
figure(hfig1);
ha1 = findobj(hfig1, 'Type', 'axes','-and','Tag','axes1');
axis(ha1);
spy
Hope that's what you wanted!
  5 Comments
p
p on 9 May 2014
But then at the beginning I also have to know how many axes I will need in my panel. And if an axes is invisible, there will be lost space in the panel where I will just see the gray background, right? Wouldn't it be the same as if I define
subplot(10, 2, 1)
at the beginning without filling all subplots later on?

Sign in to comment.


Image Analyst
Image Analyst on 9 May 2014
Set the current figure with figure(hFig1) like nl2605 told you. But you say it's still not working like you'd want. The reason is . . . you can't do subplot(1, 2, 2) and then subplot(2, 1, 2) because the layout in the second case would "overlap" the layout in the first case, causing your first plots to vanish, which is what you're seeing. In the second case you have 2 plots taking up the left and right halves of the GUI. In the second case you have 2 rows (the same) but only 1 column. So plotting, say, the bottom half with (2,1,2) will cause that plot to "overlap" the bottom half of both images that you plotted before (in the left and right half), so the first two vanish. You need to decide where to put your plots so they don't overlap if you want them all to remain.
  3 Comments
p
p on 9 May 2014
Thanks for the answer. The point is that I do not know the N by M dimension of the final plot in advance. At least not N.
Imagine you get a huge code from someone else plotting a bunch of data in different figures each having some subplots. Now you decide to add some statistic data into one of those figures by adding a new subplot at the bottom of that figure. You cannot do that but going through all the code and incrementing the N-value of each subplot that corresponds to that figure.
Isn't that very circumstantial?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by