Graphics

Copying Objects

You can copy objects from one parent to another using the copyobj function. The new object differs from the original object only in the value of its Parent property and its handle; it is otherwise a clone of the original. You can copy a number of objects to a new parent, or one object to a number of new parents, as long as the result maintains the correct parent/child relationship.

When you copy an object having child objects, MATLAB copies all children as well.

Example -- Copying Objects

Suppose you are plotting a variety of data and want to label the point having the x- and y-coordinates determined by in each plot. The text function allows you to specify the location of the label in the coordinates defined by the x- and y-axis limits, simplifying the process of locating the text.

• text('String','\{5\pi\div4, sin(5\pi\div4)\}\rightarrow',...
'Position',[5*pi/4,sin(5*pi/4),0],...
'HorizontalAlignment','right')


In this statement, the text function

• Labels the data point with the string using TeX commands to draw a right-facing arrow and mathematical symbols
• Specifies the Position in terms of the data being plotted
• Places the data point to the right of the text string by changing the HorizontalAlignment to right (the default is left)

To label the same point with the same string in another plot, copy the text using copyobj. Because the last statement did not save the handle to the text object, you can find it using findobj and the 'String' property.

• text_handle = findobj('String',...
              '\{5\pi\div4,sin(5\pi\div4)\}\rightarrow');


After creating the next plot, add the label by copying it from the first plot.

• copyobj(text_handle,gca).



This particular example takes advantage of the fact that text objects define their location in the axes data space. Therefore the text Position property did not need to change from one plot to another.

 Searching for Objects by Property Values -- findobj Deleting Objects