Search code examples
matlabplotmatlab-figuresubplot

Combine figures with subplots into one figure


Having done several simulations on a cluster, where each simulation saves a figure, I want to combine these figures into a single figure.

For ease, assume we have the two figures:

x = 0:0.01:.2;

subplot(1,3,1)
plot(x,sin(x))
legend('sin(x)')

subplot(1,3,2)
plot(x,cos(x))
legend('cos(x)')

subplot(1,3,3)
plot(x,tan(x))
legend('tan(x)')

and

x = 0:0.01:.2;

subplot(1,3,1)
plot(x,x,'r')
legend('x')

subplot(1,3,2)
plot(x,1-x.^2/2,'r')
legend('1-x.^2/2')

subplot(1,3,3)
plot(x,x,'r')
legend('x')

saved as figure1.fig and figure2.fig. I would now like to combine these two plots into a single figure with 3 subplots, the same colouring and legends. Is there an easy way to do this?


Solution

  • Open both the figures and copy the objects of one figure to the other.

    hf1 = openfig('figure1.fig');
    hf2 = openfig('figure2.fig');    set(hf2, 'Visible', 'off'); 
    
    for k=1:numel(hf1.Children)
        copyobj(hf2.Children(k).Children, hf1.Children(k));  %Copying objects to figure1
    end
    

    Result for the provided sample data is:

    output
    The plots may be too similar to be noticed which is due to the provided sample data itself.