Graphics |
Searching for Objects by Property Values -- findobj
The findobj
function provides a means to traverse the object hierarchy quickly and obtain the handles of objects having specific property values. To serve as a means of identification, all graphics objects have a Tag
property that you can set to any string. You can then search for the specific property/value pair.
For example, suppose you create a checkbox that is sometimes inactivated in the GUI. By assigning a unique value for the Tag
property, you can always find that particular instance and set its properties.
Use findobj
to locate the object whose Tag
property is set to 'save option
' and disable it.
If you do not specify a starting object, findobj
searches from the root object, finding all occurrences of the property name/property value combination that you specify.
Example -- Finding Objects
This plot of the sine function contains text objects labeling particular values of the function.
Suppose you want to move the text string labeling the value sin(t) = .707 from its current location at [pi/4,sin(pi/4)]
to the point [3*pi/4,sin(3*pi/4)]
where the function has the same value (shown grayed out in the picture). To do this, you need to determine the handle of the text object labeling that point and change its Position
property.
To use findobj
, pick a property value that uniquely identifies the object. This example uses the text String
property.
Next move the object to the new position, defining the text Position
in axes units.
findobj
also lets you restrict the search by specifying a starting point in the hierarchy, instead of beginning with the root object. This results in faster searches if there are many objects in the hierarchy. In the previous example, you know the text object of interest is in the current axes, so you can type
Example -- Using Logical Operators and Regular Expression
Suppose you create the following graph and want to modify certain properties of the objects created.
x = 0:30; y = [1.5*cos(x);4*exp(-.1*x).*cos(x);exp(.05*x).*cos(x)]'; h = stem(x,y); set(h(1),'Color','black',... 'Marker','o',... 'Tag','Decaying Exponential') set(h(2),'Color','black',... 'Marker','square',... 'Tag','Growing Exponential') set(h(3),'Color','black',... 'Marker','*',... 'Tag','Steady State')
The following instance diagram shows the graphics objects created in the graph. Each of the three sets of data produces a stemseries object, which in turn uses two lines to create the stem graph; one line for the stems and one for the markers that terminate each stem. There is also a line used for the baseline.
Controlling the Depth of the Search. Now make the baseline into a dashed line. Because it is parented directly to the axes, you can use the following statement to access only this line:
By setting -depth
to 1
, findobj
searches only the axes and its immediate children. As you can see from the above instance diagram, the baseline is the only line object parented directly to the axes.
Limiting the Search with Regular Expressions. Increase the value of the MarkerSize
property by 2 points on all stemseries objects that do not have their property Tag
set to 'Steady State'
.
h = findobj('-regexp','Tag','[^Steady State]'); set(h,{'MarkerSize'},num2cell(cell2mat(get(h,'MarkerSize'))+2))
See the regexp
function for more information on using regular expressions in MATLAB.
Using Logical Operators. Change the color of the stem lines, but not the stem markers. To do this, you must access the line objects contained by the three stemseries objects. You cannot just set the stemseries Color
property because it sets both the line and marker colors.
Search for objects that are of Type
line
, have Marker
set to none
, and do not have LineStyle
set to '--'
, which is the baseline.
The following picture shows the graph after making the various changes described in this section.
The Current Figure, Axes, and Object | Copying Objects |
© 1994-2005 The MathWorks, Inc.