How can i place saved figures into a new figure as subplots?

Hello, I am trying to place my plotted figures (saved as .fig) as subplots in one figure. The problem i face is that they axis totally changes. I would like to place them in subplots without any changes.
I tried the code at https://www.mathworks.com/matlabcentral/answers/101806-how-can-i-insert-my-matlab-figure-fig-files-into-multiple-subplots but still it changed the axis and the details of the figures. thanks

8 Kommentare

Please explain the details: "it changed the axis and the details of the figures" is to vague to understand, what you observe and what you want to get instead. Perhaps some screenshots are useful.
Attached is what i get from the matlab code. (image.png) What i would like is in image2.
These two images do not clarify, what you use as input, which code you use, why the ranges of the data are different and where the legends, titles and annotations are coming from. Please try to explain the problem more clearly, such that the readers can reproduce what you get and suggest modifications. Without seeing your code it is hard to guess, what you want to change.
The code given in that Question is not nearly a complete solution. I have posted more complete code; https://www.mathworks.com/matlabcentral/answers/262265-duplicating-an-imshow-image-into-a-new-figure-without-using-imshow#comment_332459
Hello Jan..this are figures saved as .fig from a code i have. I used the code in the link i provided to try to put them all in one figure as subplots. Image2 is what i would like them to look like (i did that in ppt but the resolution is terrible when u save it that way) and image.png is what i get when i input those figures to plot. For your convenience the code is as follows (from the link in the question)
h1 = openfig('PEA.fig','reuse'); % open figure
ax1 = gca; % get handle to axes of figure
h2 = openfig('Bar.fig','reuse');
ax2 = gca;
h3 = openfig('OP.fig','reuse');
ax3 = gca;
h4 = openfig('Re.fig','reuse');
ax4 = gca;
h5 = openfig('Thu.fig','reuse');
ax5 = gca;
h6 = openfig('H.fig','reuse');
ax6 = gca;
h7 = figure; %create new figure
s1 = subplot(2,3,1); %create and get handle to the subplot axes
s2 = subplot(2,3,2);
s3 = subplot(2,3,3);
s4 = subplot(2,3,4);
s5 = subplot(2,3,5);
s6 = subplot(2,3,6);
fig1 = get(ax1,'children'); %get handle to all the children in the figure
fig2 = get(ax2,'children');
fig3 = get(ax3,'children');
fig4 = get(ax4,'children');
fig5 = get(ax5,'children');
fig6 = get(ax6,'children');
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
copyobj(fig2,s2);
copyobj(fig3,s3);
copyobj(fig4,s4);
copyobj(fig5,s5);
copyobj(fig6,s6);
copyobj of the axes children is not enough: you need to copy the axes properties as well.
Thanks Walter, that makes sense. Can you guide me as in how to do that exactly...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 20 Okt. 2018
Bearbeitet: Jan am 20 Okt. 2018
I assume, all you have to do is to replace:
h7 = figure; %create new figure
s1 = subplot(2,3,1); %create and get handle to the subplot axes
fig1 = get(ax1,'children'); %get handle to all the children in the figure
copyobj(fig1,s1); %copy children to new parent axes i.e. the subplot axes
by
h7 = figure; %create new figure
newAxes = copyobj(ax1, h7); %copy axes to new figure
If you want to change the position of the new axes object, do this afterwards.

4 Kommentare

Thanks Jan, but how do you put those figures in subplots? Replacing the first four lines with the two below just replaces the graphs in the new figure. One could just repeat those two lines for all the figures (using hold on) then manually play with the figure positions, generate the code and then add that to the main one!! Hope you have a better idea :)
Walter Roberson
Walter Roberson am 25 Okt. 2018
Bearbeitet: Walter Roberson am 25 Okt. 2018
You would use multiple copies of the second line to copy all of the axes into the h7 figure. You would not need "hold on".
You would have to set the Position properties after doing the copying.
Note: this approach is likely to mess up legends, and possibly colorbar plots as well.
@Yasmin Samy: To be exact, you cannot "put a figure in a subplot". While a "figure" is the complete window with title bar and menus, a "subplot" is an axes object. Does "playing with the figure position" mean the position of the axes? Confusing.
@Walter Roberson: Thanks for your comment, i guess plotting them all in the same figure where they overlap each other, then use Position to place them in the required place (and resize them as well). Thanks for that.
@Jan: Yes i meant using Position to place them where i want them instead of using subplot. I tried doing that with 2 plots, having them plotted (they overlap each other) full size in the same figure. Then i manually resized and put them in the place i would want them to be (manually doing what i was hoping 'subplot' would do).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 18 Okt. 2018

Kommentiert:

am 26 Okt. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by