# How to copy two figures to a single figure without removing properties of original figures?

10 views (last 30 days)
MathWorks Support Team on 1 Dec 2022
Suppose I have two ".fig" figure files: "Fig1.fig" and "Fig2.fig". I am using the following code to create a single figure that contains both figures:
% Prepare subplots
figure
h(1)=subplot(1,2,1);
h(2)=subplot(1,2,2);
% Paste figures on the subplots
copyobj(allchild(get(Fig1,'CurrentAxes')),h(1));
copyobj(allchild(get(Fig2,'CurrentAxes')),h(2));
However, the figures copied to the subplots are not displayed correctly. How can I copy all the relevant properties (such as colors, color bar, colors scale, x and y labels, and 3D orientation ) from my original figures to create a single figure that displays the original figures as they are?

MathWorks Support Team on 1 Dec 2022
Please note that there is no straightforward way to directly copy all the relevant properties in this scenario. To explain the reason for this limitation, the "copyobj" command you have used is split into separate commands:
h(1) = subplot(1,2,1);
ca = get(Fig1,'CurrentAxes'); % Returns the axes within the figure (there is only one axes in each figure).
child = allchild(ca); % Returns the children of that axes. The axes in “Fig1” has one child (a patch). The axes in “Fig2” has one child (an image).
copyobj(child, h(1)) % Copy the single patch or image into the new axes created by subplot.
Most of the properties that need to be copied (including the view angle of the 3D axes and the colormap) are stored on the axes and figure, not the patch or image, and this approach copies only the patch or image, not the axes. Also note that you cannot set the parent of axes to be other axes, so you cannot “copyobj” an axes into another axes (and the output from subplot is an axes).
However, there are several workarounds to accomplish this task. Please choose the option that suites the most to your requirements.
Option 1
In this approach, you can leverage “tiledlayout” in the new figure instead of “subplot”. Please note that copying the “Colormap” property still needs to be done manually. This is because,
1. When you are condensing two figures into one, there is no automated approach to know whether to keep properties from “Fig1” or “Fig2” (so some manual intervention is required),
2. There is no automated way to copy all properties from one existing figure to another existing figure (copyobj will copy properties, but it creates a new figure).
% Prepare the new figure and layout
f = figure;
t = tiledlayout(f,1,2);
% Paste figures on the tiles
child1 = copyobj(Fig1.Children, t);
child2 = copyobj(Fig2.Children, t);
ax2 = findobj(child2, 'type', 'axes');
ax2.Layout.Tile = 2; % Move the axes to the right tile.
ax1 = findobj(child1, 'type', 'axes');
% manually set the Colormap property of "Axes" in each panel
ax1.Colormap = Fig1.Colormap;
ax2.Colormap = Fig2.Colormap;
The above approach will yield the expected figure displaying the original figures with properties copied correctly.
Option 2
Another approach would be to use panels, but that requires a bit more manual layout.
% Prepare new figure and panels
f = figure;
p1 = uipanel(f, 'OuterPosition', [0 0 0.5 1], 'BorderType','none');
p2 = uipanel(f, 'OuterPosition', [0.5 0 0.5 1], 'BorderType','none');
% Paste figures on the subplots
copyobj(Fig1.Children, p1);
copyobj(Fig2.Children, p2);
% manually set the Colormap property of "Axes" in each panel
p1.Children(2).Colormap = Fig1.Colormap;
p2.Children(2).Colormap = Fig2.Colormap;

Option 3
If you have control over how the original “Fig1” and “Fig2” are created, you can use a “tiledlayout” object as the parent of the axes in those figures. Then, you can “copyobj” the entire “tiledlayout” from the figure into the “tiledlayout” of another figure. Note that "tiledlayout" objects can be nested (unlike "subplot"), so you can put one "tiledlayout" inside another "tiledlayout" object. That is what allows this approach to work. In this approach you can also specify the colormap on the axes instead of the figure, which means the colormap will be associated with the axes and will be copied along with the axes.
The following code snippet demonstrates this idea:
f1 = figure;
t1 = tiledlayout(f1,1,1);
ax = nexttile(t1);
surf(ax, peaks);
colorbar(ax);
colormap(ax, 'winter'); % Colormap command specifying axes as input
f2 = figure;
t2 = tiledlayout(f2,1,1);
ax = nexttile(t2);
imagesc(ax, peaks);
colorbar(ax);
colormap(ax, 'summer'); % Colormap command specifying axes as input
f3 = figure;
t3 = tiledlayout(f3,1,2);
t1copy = copyobj(f1.Children, t3);
t2copy = copyobj(f2.Children, t3);
t2copy.Layout.Tile = 2; % Move the contents of the second figure to the second tile.

### Categories

Find more on Colormaps in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by