MATLAB: Duplicate plots into subplot

plotsubplot

I am using a code that generates a series of plot, each having their own full scale figure. I'd like to know if there is a way to generate an additional figure with subplot using simply the subplot command and the handles of the previous plots ?
Instead of repeating the plot instruction twice (one for the main figure, and one for the copy in the subplot)
Ideally something like (with my figures handles being fig_i) subplot(2,2,1,'fig1') subplot(2,2,2,'fig2') etc
is this supported ?

Best Answer

Consider using copyobj which will allow you to copy a graphics object from one parent to another. Suppose you have the following figure which displays a single axes with the sine curve drawn on it
figure;
x = linspace(-2*pi,2*pi,500);
y = sin(x);
hCurve = plot(x,y,'r');
So we create a figure and draw the sine curve to it. The output from plot is a handle to the graphics object, and is saved to hCurve.
In a second figure we create the figure and two sup lots
figure;
hSub1 = subplot(2,1,1);
hSub2 = subplot(2,1,2);
hSub1 and hSub2 are handles to the axes of each subplot. We then copy the curve to the first axes as
copyobj(hCurve,hSub1);
And we see that the sine curve has been copied over.
You can do something similar starting with your fig_i handle. Since it is a handle to a figure, then you want to first get the handle to the axes on that figure (let's assume that you just have the one axes). You can do this as
hFigIAxes = findobj('Parent',fig_i,'Type','axes');
So the above searches for any graphics object whose parent is the handle fig_i and whose type is axes. We then can copy the axes children (which are the graphics objects drawn on it) to the subplot
if ~isempty(hFigIAxes)
hAxes = hFigIAxes(1); % assume just the one axes
copyobj(get(hAxes,'Children'),hSub1);
end
Try the above and see what happens!