Copy graphics objects and their descendants
new_handle = copyobj(h,p)
copyobj creates copies of graphics objects
and assigns the objects to the new parent.
The new parent must be appropriate for the copied object (for
example, you can copy an axes only to figure or uipanel).
children as well.
new_handle = copyobj(h,p) copies
one or more graphics objects identified by
returns the handle of the new object or an array of new objects. The
new graphics objects are children of the graphics objects specified
object callback properties and object application data. This behavior
is consistent with versions of
copyobj before MATLAB® release
What is Not Copied
copyobj does not copy properties or objects
that depend on their original context to operate properly. Objects
with default context menus (such as legends and colorbars) create
new context menus for the new object. Figures create new toolbars
and menus for the new figure.
copyobj does not copy:
Callback properties (except when using the
Application data associated with the object (except when using the
Context menu of legends, colorbars, or other objects that define default context menus.
Default figure toolbar and menus.
Axes objects used with the
Interactionsproperty of an axes object.
DataTipTemplateproperty for objects that have this property, such as
You cannot copy the same object more than once to the same parent in a single call to
MATLAB changes the
Parent property to
the new parent and assigns the new objects a new handle.
Copy a surface to a new axes that is in a different figure.
h = surf(peaks); colormap hsv
Create the destination figure and axes:
fig = figure; ax = axes;
Copy the surface to the new axes and set properties that are not surface properties:
new_handle = copyobj(h,ax); colormap(fig,hsv) view(ax,3) grid(ax,'on')
Note that while the surface is copied, the
grid are not copied.
p can be scalars
or vectors. When both are vectors, they must be the same length, and
the output argument,
new_handle, is a vector of
the same length. In this case,
a copy of
h(i) with its
h is a scalar and
h is copied once to each of the parents
a copy of
h with its
h is a vector and
a scalar, each
new_handle(i) is a copy of
Parent property set to
The length of
You must copy the associated axes when copying a legend or a colorbar.
When programming a UI, do not call
textwrap (which calls
copyobj) inside a
CreateFcn. The act of copying the uicontrol object fires the
CreateFcn repeatedly, which raises a series of error messages
after exceeding the recursion limit.
Introduced before R2006a