How to merge two figure files into a single file

Acorbe picture Acorbe · Nov 7, 2012 · Viewed 42.3k times · Source

This should be a problem with a trivial solution, but still I wasn't able to find one.

Say that I have 2 matlab figures fig1.fig, fig2.fig which I want to load and show in the same plotting window.

What should I do?

I mean, I am pretty sure that I can accomplish the task using some low(er) level graphic command which extracts contents from one image and put them in the second one, nonetheless I cannot believe that there is not any high level function (load fig2 on top of fig1) that does this...Comparing 2 plots (unfortunately already saved) is a very common task, I'd say.

Answer

slayton picture slayton · Nov 7, 2012

Its not clear if you want to extract data from the figures and compare the data, or if you want to combine the plots from two figures into a single figure.

Here is how you combine two figures into one (if thats what you want to do)..

First load the figures:

fig1 = open('FigureFile1.fig');
fig2 = open('FigureFile2.fig');

Get the axes objects from the figures

ax1 = get(fig1, 'Children');
ax2 = get(fig2, 'Children');

Now copy the hangle graphics objects from ax2 to ax1. The loop isn't neccesary if your figures only have a single axes

for i = 1 : numel(ax2) 
   ax2Children = get(ax2(i),'Children');
   copyobj(ax2Children, ax1(i));
end

Note This example assumes that your figures have the same nubmer of axes and that you want to copy objects from the first axes in the second figure to the first axes on the first figure. Its up to you to figure out the proper indexing if the axes indices aren't lined up.