How can I put existing figures in different subplots in another figure in MATLAB 6.5 (R13)?
203 views (last 30 days)
Show older comments
MathWorks Support Team
on 8 Sep 2010
Commented: Walter Roberson
on 25 Aug 2023
I want to make several plots, each in their own figure. I then want to create a final figure which contains subplots which have the contents of the original figures.
Accepted Answer
MathWorks Support Team
on 8 Sep 2010
The ability to make subplots from a set of figures interactively has been incorporated in MATLAB 7.2 (R2006a) using 'Plot Tools'. To do this, perform the following steps:
1. Plot a figure.
2. Click on the white icon, "Show Plot Tools and dock figure", on the top of the figure.
3. Select all objects by pressing CTRL-A
4. Copy the objects by pressing CTRL-C
5. Open a new figure window in Plot Tools by clicking on the white icon, "New Figure" on the left.
6. Paste the objects by pressing CTRL-V
7. Repeat the steps 1-4 for the other figures you want to copy and paste them into the final figure.
The sub-plots in the final figure can now be resized and moved as desired.
To work around this issue in previous releases, read the following:
The COPYOBJ function will allow you to copy objects between parent objects. In order to copy several sets of axes into a subplot, you will need to use two steps:
1. Copy the contents of your original figure into your destination figure.
2. Modify the position properties of the axes so that they match a subplot's position. Following is an example code which should help you to set the position property. This code is designed only to work with a 4-by-1 set of subplots. You may need to modify the code to work with your data.
% First, create 4 figures with four different graphs (each with a
% colorbar):
figure(1)
surf(peaks(10))
colorbar
figure(2)
mesh(peaks(10))
colorbar
figure(3)
contour(peaks(10))
colorbar
figure(4)
pcolor(peaks(10))
colorbar
% Now create destination graph
figure(5)
ax = zeros(4,1);
for i = 1:4
ax(i)=subplot(4,1,i);
end
% Now copy contents of each figure over to destination figure
% Modify position of each axes as it is transferred
for i = 1:4
figure(i)
h = get(gcf,'Children');
newh = copyobj(h,5)
for j = 1:length(newh)
posnewh = get(newh(j),'Position');
possub = get(ax(i),'Position');
set(newh(j),'Position',...
[posnewh(1) possub(2) posnewh(3) possub(4)])
end
delete(ax(i));
end
figure(5)
2 Comments
Walter Roberson
on 25 Aug 2023
@Megna Hari comments:
This doesnt work for an mxn layout where n>1.
it should be this but theres some reputation points requirement now so I cant post:
set(newh(j),'Position',[possub(1)+(posnewh(1)*possub(3)), possub(2)+(posnewh(2)*possub(4)), posnewh(3)*possub(3), posnewh(4)*possub(4)])
More Answers (1)
Eric Sargent
on 9 Dec 2020
Starting in R2019b, you can use tiledlayout to manage several axes. You can reparent each of your axes into a layout, and set the tile number of each axes as appropriate.
% Get a list of all of the open figures
figlist=get(groot,'Children');
newfig=figure;
tcl=tiledlayout(newfig,'flow')
for i = 1:numel(figlist)
figure(figlist(i));
ax=gca;
ax.Parent=tcl;
ax.Layout.Tile=i;
end
See Also
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!