MATLAB Answers

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

22 views (last 30 days)
Haitham on 25 Aug 2015
Commented: Haitham on 29 Aug 2015
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
Error in graphics.internal.HG1ListenerInterface
Error while evaluating uicontrol Callback
Thank you

  1 Comment

Haitham on 25 Aug 2015
I have tried to make the code of the copy is in the callback of the first GUI as the following:
move_from = figure(20);
move_to = GUI_results_screen();
axchild = findall(move_from, 'type', 'axes');
set(axchild, 'Parent', move_to);
and it works without any errors, but the graph was copied in the middle of the second GUI not inside the axis. I have tried to call the axis by
before the "set" statement but that didn't work either. Any help?!

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 25 Aug 2015
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
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
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
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
Try the above and see what happens!


Show 2 older comments
Haitham on 26 Aug 2015
Actually, I'm trying to copy four axes each one in a separate figure window into four axes within one GUI. But now I'm trying to copy just one of them into one of the axes.
So, it can be done? or I have to change the four axes (which are within the GUI) into uipanels.
Haitham on 26 Aug 2015
I have tried to get the handles of the second GUI then get the handle of the axis from it by this code:
GUI2_handels = GUI_results_screen(handles);
hFigAxes_From = findobj('Parent',figure(20),'Type','axes');
hGUIAxes_To = axes(GUI2_handels.axes1);
But I have got this error:
Attempt to reference field of non-structure array.
Error in GUI_Main>Button_Calculate_Callback (line 4230)
hGUIAxes_To = axes(GUI2_handels.axes1);
Error in gui_mainfcn (line 95)
Error in GUI_Main (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
Error while evaluating uicontrol Callback
So does that mean that dealing with the second GUI handles can't be done unless the second GUI has to be open?! I'm getting confused!
Haitham on 29 Aug 2015
Thank you Geoff, I ended up with putting the callback in the second GUI, so all the needed codes are placed in the second GUI then I used your code. It works. Thanks

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!