Main Content

copyobj

Copy graphics objects and their children

    Description

    newobj = copyobj(obj,parent) creates a copy of the specified graphics object, assigns it to the specified new parent, and returns the new object. If obj has children, copyobj copies them as well.

    copyobj does not copy properties or child objects that depend on their original context, including callback properties or any application data associated with the original object. For more information, see What Is Not Copied.

    example

    newobj = copyobj(obj,parent,"legacy") additionally copies the object callback properties and object application data. This behavior is consistent with copyobj functionality before R2014b.

    Examples

    collapse all

    Copy a surface object to new axes in a different figure to view the plot from two angles simultaneously.

    Create a surface object.

    obj = surf(peaks);

    Figure contains an axes object. The axes object contains an object of type surface.

    Create a new figure and axes. Then, create a copy of the surface object and assign it to the new axes. Change the view of the new plot by setting the line of sight of the camera to an azimuth of 60 degrees and an elevation of 30 degrees. Turn the grid on to match the original figure configuration, which is not copied with the surface object.

    fig = figure;
    ax = axes;
    newobj = copyobj(obj,ax);
    view(ax,60,30)
    grid(ax,"on")

    Figure contains an axes object. The axes object contains an object of type surface.

    Create a set of similar plots, and label the same data point on each plot by copying a text object and a line marker object from the first plot to each of the other plots.

    Create the first plot, and label it using a text object and a line marker object.

    x = 0:.1:2*pi;
    y = sin(x);
    plot(x,y)
    title("Sine Plot")
    t = text(5*pi/4, -sqrt(2)/2, "\{5*pi/4,-sqrt(2)/2\}\rightarrow", ...
       "HorizontalAlignment","right");
    lm = xline(5*pi/4);

    Figure contains an axes object. The axes object with title Sine Plot contains 3 objects of type line, text, constantline.

    Create a second plot without labels, and copy the text and line marker objects (t and lm) to the plot by assigning them to the new axes. Return the text and line marker object copies.

    figure
    x = pi/4:.1:9*pi/4;
    plot(x,cos(x))
    title("Cosine Plot")
    ax2 = gca;
    ax2.Color = [0.902 0.902 0.902]; % light gray
    lbl2 = copyobj([t,lm],ax2);

    Figure contains an axes object. The axes object with title Cosine Plot contains 3 objects of type line, constantline, text.

    Copy the objects from several figures into one figure that uses a tiled chart layout.

    Create the first figure containing a sine plot.

    f1 = figure;
    ax1 = axes(f1);
    plot(ax1,sin(1:10))

    Figure contains an axes object. The axes object contains an object of type line.

    Create the second figure containing four plotted lines and a legend.

    f2 = figure;
    ax2 = axes(f2);
    plot(ax2,magic(4));
    lgd = legend(ax2);

    Figure contains an axes object. The axes object contains 4 objects of type line.

    Create a third figure with a tiled chart layout, and copy the plots and legend from the first and second figures into the tiles in the third figure.

    f3 = figure;
    t = tiledlayout(f3,2,1);
    
    ax1c = copyobj(ax1,t);
    ax2c = copyobj([ax2,lgd],t);
    ax1c(1).Layout.Tile = 1;
    ax2c(1).Layout.Tile = 2;

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains 4 objects of type line.

    Copy the objects from different tiles in a tiled chart layout into multiple figures.

    Create a figure containing a 2-by-1 tiled chart layout with two plots. The first tile contains a sine plot, and the second tile contains four plotted lines.

    f = figure;
    t = tiledlayout(f,2,1);
    
    ax1 = nexttile(t);
    plot(ax1,sin(1:10))
    
    ax2 = nexttile(t);
    plot(ax2,magic(4))

    Figure contains 2 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains 4 objects of type line.

    Create a new figure, and copy the axes from the first tile into the new figure. To allow the axes to fill the entire figure, reset the position of the axes back to the default value.

    f1 = figure;
    ax1c = copyobj(ax1,f1);
    set(ax1c,"OuterPosition","default");

    Figure contains an axes object. The axes object contains an object of type line.

    Create another new figure, and copy the axes from the second tile into the new figure. To allow the axes to fill the entire figure, reset the position of the axes back to the default value.

    f2 = figure;
    ax2c = copyobj(ax2,f2);
    set(ax2c,"OuterPosition","default")

    Figure contains an axes object. The axes object contains 4 objects of type line.

    Copy a panel containing multiple user interface controls from one figure to another.

    Create a figure and a panel positioned within it. Then, create two radio buttons within the panel and set their values.

    f1 = figure;
    p1 = uipanel(f1,"Position",[0.1 0.1 0.35 0.65]);
    rb1 = uicontrol(p1,"Style","radiobutton","String","Option 1", ...
        "Position",[10 50 100 22]);
    rb2 = uicontrol(p1,"Style","radiobutton","String","Option 2", ...
        "Position",[10 28 100 22]);
    p1.Children(1).Value = 0;
    p1.Children(2).Value = 1;

    Figure contains an object of type uipanel.

    Create a new figure with a darker background, and copy the panel from the first figure into the second figure. Then, change the values of the radio buttons in the second figure.

    f2 = figure;
    f2.Color = [0.902 0.902 0.902];
    p2 = copyobj(p1,f2);
    p2.Children(1).Value = 1;
    p2.Children(2).Value = 0;

    Figure contains an object of type uipanel.

    Input Arguments

    collapse all

    Graphics object to copy, specified as a single object or a vector of objects.

    Note

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

    Parent of copied object, specified as a single object or a vector of objects. The parent must be appropriate for the copied object. For example, you can copy a line into axes, but not into a figure or panel.

    Output Arguments

    collapse all

    Copy of graphics object, returned as a single object or a vector of objects.

    • If obj is a single object and parent is a single object, newobj is a single object.

    • If obj is a single object and parent is a vector of objects, newobj is a vector of objects. Each newobj(i) is a copy of obj assigned to the parent parent(i). The length of newobj equals the length of parent.

    • If obj is a vector of objects and parent is a single object, newobj is a vector of objects. Each newobj(i) is a copy of obj(i) assigned to the parent parent. The length of newobj equals the length of obj.

    • If both obj and parent are vectors of objects, they must be the same length, and newobj is a vector of objects of the same length. Each newobj(i) is a copy of obj(i) assigned to the parent parent(i).

    The copied object is different from the original object. Therefore, == returns false when comparing the two objects. For more information, see What Is Not Copied.

    More About

    collapse all

    What Is Not Copied

    When creating a copy of a graphics object, copyobj does not copy properties or child objects that depend on their original context. If an object has default context menus (like as legends and colorbars have), then the copied object gets new context menus. Copies of figures get new toolbars and menus.

    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

    Tips

    • When copying a legend or a colorbar, you must also copy the associated axes.

    • When building a user interface, do not call copyobj or textwrap (which calls copyobj) inside a creation function. Copying a user interface control object calls the creation function repeatedly, exceeding the recursion limit and therefore causing an error.

    Version History

    Introduced before R2006a

    See Also

    Functions