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

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)

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

Thanks a lot! Now I am a bit closer to the solution:
hfig1 = figure;
subplot(2, 2, 1,'Tag','axes1');
plot(1, 1);
subplot(2, 2, 2,'Tag','axes2');
plot(1, 1);
hfig2 = figure;
plot(1, 1);
figure(hfig1);
hold on;
subplot(2, 1, 2, 'Tag', 'axes3');
spy
This is just doing what I wanted to do, but I want to use
subplot(1, 2, 1,'Tag','axes1');
subplot(1, 2, 2,'Tag','axes2');
for the first subplots (note that the column number is 1 and not 2) because at the beginning I don't know how many subplots will be added into the first figure. That being said, it should look 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);
hold on;
subplot(2, 1, 2, 'Tag', 'axes3');
spy
But now the first two subplots are gone. Maybe there is a way to add a new axes to the fig1 specifying the position and after that drawing a new subplot to this axes?
I think by giving the position in the subplot and using 'align' maybe what you want can be achieved. Go for help subplot.
Sorry, but I don't see where 'align' could help me. I tried
subplot(2, 1, 2, 'align');
instead of the third subplot, but it didn't work. Maybe I didn't get your idea? I just want to add a new subplot without disappearing of the ones before.
I tried myself. Its not working. I am not sure if one can add subplots dynamically. However, what you can do is have a panel with axes in it. And when you want it to appear you can do it by setting the 'Visible' property. Else, the axes remains hidden.
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.

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

So there is no way to add subplots dynamically without knowing the amount of subplots at the beginning?
Well sort of correct. You can add them dynamically but you have to know the N by M layout of the plots in advance because that's the first two arguments of subplot. And you need to be careful about the layout if you don't want to blow away prior plots. The layout you select can be different or can be the same but you have to be careful. So, yes, you can add dynamically, but with that caveat.
subplot(2,2,1);
plot(1:5, 'bs-');
subplot(2,2,2);
plot(1:15, 'rs-');
% Plot again with different layout but no overlap
% Use "slots" 3 and 4 for a wide plot.
subplot(2,2,3:4);
plot(1:5, 'kd-');
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.

Asked:

p
p
on 7 May 2014

Commented:

p
p
on 9 May 2014

Community Treasure Hunt

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

Start Hunting!