Thread Subject: copyobj from existing figure into GUI axes

Subject: copyobj from existing figure into GUI axes

From: Dave Brackett

Date: 29 Nov, 2008 22:24:01

Message: 1 of 5

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.

Subject: copyobj from existing figure into GUI axes

From: Bruno Luong

Date: 30 Nov, 2008 08:29:05

Message: 2 of 5

"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 cannot copyobj directly from figure to axes. You might split the process into 2 steps: (i) copy a source figure into a temporary (invisible) figure, then (ii) copy the axes of the temporary figure into a final destination axes.

Bruno

Subject: copyobj from existing figure into GUI axes

From: Ryan Ollos

Date: 30 Nov, 2008 09:39:02

Message: 3 of 5

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggtish$4u3$1@fred.mathworks.com>...
> "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 cannot copyobj directly from figure to axes. You might split the process into 2 steps: (i) copy a source figure into a temporary (invisible) figure, then (ii) copy the axes of the temporary figure into a final destination axes.
>
> Bruno

I don't see how this will work. He is copying the children of a figure, which is presumably just an axes, to a new axes. However, with the copyobj function you must copy an object to a new parent. So you can copy:
axes -> figure
line -> axes
But you can't copy axes -> axes.

Subject: copyobj from existing figure into GUI axes

From: Ryan Ollos

Date: 30 Nov, 2008 09:43:02

Message: 4 of 5

"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.

Subject: copyobj from existing figure into GUI axes

From: Bruno Luong

Date: 30 Nov, 2008 10:04:01

Message: 5 of 5

"Ryan Ollos" <ryano@physiosonics.com> wrote in message <ggtmvm$2qd$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ggtish$4u3$1@fred.mathworks.com>...
> > "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 cannot copyobj directly from figure to axes. You might split the process into 2 steps: (i) copy a source figure into a temporary (invisible) figure, then (ii) copy the axes of the temporary figure into a final destination axes.
> >
> > Bruno
>
> I don't see how this will work. He is copying the children of a figure, which is presumably just an axes, to a new axes. However, with the copyobj function you must copy an object to a new parent. So you can copy:
> axes -> figure
> line -> axes
> But you can't copy axes -> axes.
>

Sorry for the confusion, a little code is better than the "blah blah"

% Generate a fig file
close all;
figure(1);
clf(1);
axsource=axes('Parent',1);
plot(axsource,rand(10,1));
hgsave(1,'sourcefig.fig');
close all;

% Load the fig file
h=hgload('sourcefig.fig');
set(h,'Visible','off');
tmpaxes=findobj(h,'Type','axes');

% Copy the axe
figure(2);
destaxes=subplot(2,2,1,'Parent',2)
copyobj(allchild(tmpaxes),destaxes);

% Clean up
close(h)

% Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
copyobj Dave Brackett 29 Nov, 2008 17:25:05
guide Dave Brackett 29 Nov, 2008 17:25:05
gui Dave Brackett 29 Nov, 2008 17:25:05
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com