Main Content


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). copyobj copies children as well.

new_handle = copyobj(h,p) copies one or more graphics objects identified by h and returns the handle of the new object or an array of new objects. The new graphics objects are children of the graphics objects specified by p.

copyobj(___,'legacy') copies object callback properties and object application data. This behavior is consistent with versions of copyobj before MATLAB® release R2014b.

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 legacy option).

  • Application data associated with the object (except when using the legacy option).

  • Context menu of legends, colorbars, or other objects that define default context menus.

  • Default figure toolbar and menus.

  • Axes objects used with the yyaxis function.

  • The Interactions property of an axes object.

  • The DataTipTemplate property for objects that have this property, such as Line, Scatter, and Surface objects.

  • You cannot copy the same object more than once to the same parent in a single call to copyobj.

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);

Note that while the surface is copied, the colormap, view, and grid are not copied.


h and 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, new_handle(i) is a copy of h(i) with its Parent property set to p(i).

When h is a scalar and p is a vector, h is copied once to each of the parents in p. Each new_handle(i) is a copy of h with its Parent property set to p(i), and length(new_handle) equals length(p).

When h is a vector and p is a scalar, each new_handle(i) is a copy of h(i) with its Parent property set to p. The length of new_handle equals length(h).


You must copy the associated axes when copying a legend or a colorbar.

When programming a UI, do not call copyobj or 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.

Version History

Introduced before R2006a

See Also

| | | | |