MATLAB: How to copy a graph from a figure window to an axis within a GUI

copy figure guiMATLAB

I have a graph that plotted from the first GUI and that graph is in a separate figure window. I am trying to copy that graph from that separate figure window to an axis within the second GUI. I have got a help in this issue but there is an error and I am not sure how to fix it. This is the code: In the callback of the first GUI:
move_from = figure(20); % my figure window that I want to copy
axchild = findall(move_from, 'type', 'axes');
GUI_results_screen % open the second GUI
In the second GUI:
move_to = GUI_results_screen();
set(axchild, 'Parent', move_to);
The error that I got is: (after several of flashing from the second GUI)
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the
limit. Be aware that exceeding your available stack space can crash MATLAB and/or your
computer.
Error in graphics.internal.HG1ListenerInterface
Error while evaluating uicontrol Callback
Thank you

Best Answer

Haitham - 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!