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

3 次查看(过去 30 天)
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
  1 个评论
Haitham
Haitham 2015-8-25
编辑:Haitham 2015-8-25
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
axes(handles.axes1);
before the "set" statement but that didn't work either. Any help?!

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2015-8-25
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!
  5 个评论
Haitham
Haitham 2015-8-26
编辑:Haitham 2015-8-26
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);
copyobj(hFigAxes_From,hGUIAxes_To);
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)
feval(varargin{:});
Error in GUI_Main (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)GUI_Main('Button_Calculate_Callback',hObject,eventdata,guidata(hObject))
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
Haitham 2015-8-29
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by