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

17 vues (au cours des 30 derniers jours)
p
p le 7 Mai 2014
Commenté : p le 9 Mai 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 ...

Réponses (2)

nl2605
nl2605 le 8 Mai 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 commentaires
nl2605
nl2605 le 9 Mai 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.
p
p le 9 Mai 2014
Modifié(e) : p le 9 Mai 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?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 9 Mai 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 commentaires
Image Analyst
Image Analyst le 9 Mai 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-');
p
p le 9 Mai 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?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by