Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: copyobj from existing figure into GUI axes
Date: Sun, 30 Nov 2008 09:43:02 +0000 (UTC)
Organization: PhysioSonics Inc
Lines: 30
Message-ID: <ggtn76$8oe$1@fred.mathworks.com>
References: <ggsfe1$e3d$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228038182 8974 172.30.248.37 (30 Nov 2008 09:43:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 30 Nov 2008 09:43:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1129061
Xref: news.mathworks.com comp.soft-sys.matlab:503886


"Dave Brackett" <davebrackett@hotmail.com> wrote in message <ggsfe1$e3d$1@fred.mathworks.com>...
> Hi, I am trying to display a previously created figure in a GUI axes. I think I need to use the copyobj function and have written the below lines of code, but I can't get it to work:
> 
> [FileName,PathName]=uigetfile('.fig','Select a figure'); % user selects the figure
> figure_handle = openfig(strcat(PathName,FileName)); % figure is opened
> h=handles.axes1; % handle to axes in GUI
> copyobj(allchild(figure_handle),h);  % original figure is copied to new figure
> 
> It returns this error: 
> ??? Error using ==> copyobj
> Object uicontextmenu[1] can not be a child of parent axes[1]
> Error in ==> figure_viewer>pushbutton1_Callback at 100
> copyobj(allchild(figure_handle),h);
> 
> I can't work out what I need to do to resolve this, but I have found that if I replace 'h=handles.axes1' with 'h=figure' it works and correctly copies the original figure to a new figure, but I want it to copy it to the axes within the GUI.
> 
> Any ideas? Thanks.

You can't copy an axes to an axes, which is what you are trying to do.  If you want to copy the children of the axes , and you have only one axes in the figure, the following should work:

figure_handle = openfig(strcat(PathName,FileName)); % figure is opened
axes_handle = findobj(figure_handle, 'Type', 'Axes'); % find handle to axes in figure
axes_children_handle = get(axes_handle, 'Children');
copyobj(axes_children_handle, handles.axes1);  % children of original axes are copied to new axes

If there are multiple axes in the figure you are opening, then you just need a Tag or other unique property in order to find the handle to the object.

However, this doesn't copy the properties of the axes, so I think a better solution would be to copy the axes to your destination figure, and then set the properties of the axes in the destination figure.

I'm assuming you are using GUIDE to create your destination figure, and in GUIDE you don't need to create a axes since it will be copied.  After you copy the axes you can set the Position and other properties so that it displays correctly in your destination GUI.