Copy graphics objects and their descendants
new_handle = copyobj(h,p)
copyobj creates copies of graphics objects. The copies are identical to the original objects except the copies have different values for their Parent property and a new handle. The new parent must be appropriate for the copied object (e.g., you can copy a line object only to another axes object).
new_handle = copyobj(h,p) copies one or more graphics objects identified by h and returns the handle of the new object or a vector of handles to new objects. The new graphics objects are children of the graphics objects specified by p.
Copy a surface to a new axes within a different figure.
h = surf(peaks); colormap hot figure % Create a new figure axes % Create an axes object in the figure new_handle = copyobj(h,gca); colormap hot view(3) grid on
Note that while the surface is copied, the colormap (figure property), view, and grid (axes properties) 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).
When programming a GUI, 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 root object's RecursionLimit property.