Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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.

Contact us