How to set parent figure of subplot which is not the current figure?
15 views (last 30 days)
Show older comments
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 ...
0 Comments
Answers (2)
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
nl2605
on 9 May 2014
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.
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
Image Analyst
on 9 May 2014
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-');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178679/image.png)
See Also
Categories
Find more on Subplots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!