Main Content

findobj

Find graphics objects with specific properties

Description

example

h = findobjreturns the graphics root object and all of its descendants.

example

h = findobj(prop,value)returns all objects in the hierarchy that have their propertypropset tovalue.

example

h = findobj('-not',prop,value)returns all objects whose specified property is not set to the specified value.

example

h = findobj(prop1,value1,oper,prop2,value2)applies the logical operatoroperto theprop,valuepairs. For example,h = findobj('LineStyle','--','-and','Marker','o')returns all objects that have a dashed line style and circular markers.

example

h = findobj('-regexp',prop,expr)uses a regular expression to find objects with specific property values. Objects with property values satisfying the regular expression are returned.

example

h = findobj('-property',prop)returns all objects that have the specified property.

example

h = findobj(prop1,value1,...,propN,valueN)returns all objects in the hierarchy that have the specified properties set to the specified values. You can replaceprop,valuepairs with other input argument combinations from the previous syntaxes. For example,h = findobj(prop1,value1,'-not',prop2,value2,'-property',prop3)returns all objects that satisfy these three conditions:

  • The object has a propertyprop1set tovalue1.

  • The object has a propertyprop2whose value is not set tovalue2.

  • The object has a propertyprop3.

example

h = findobj(objhandles,___)restricts the search to the objects listed inobjhandlesand all of their descendants. You can restrict the search for any of the previous syntaxes.

example

h = findobj(objhandles,'-depth',d,___)restricts the search to the objects listed inobjhandlesand their descendants that are up todlevels lower in the graphics object hierarchy.

example

h = findobj(objhandles,'flat',___)restricts the search to the objects listed only inobjhandles. The descendant objects are not searched. Using the'flat'option is the same as using the'-depth'option withd = 0.

Examples

collapse all

Delete all existing figures, and then create a plot of random values.

closeallplot(rand(5))

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

Return the graphics root object and all of its descendants.

h = findobj
h = 8x1 graphics array: Root Figure (1) Axes Line Line Line Line Line

Delete all existing figures, and then create a multiline plot.

closeallplot(magic(4))

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

Return all line objects.

h = findobj('Type','line')
h = 4x1 Line array: Line Line Line Line

Plot nine sine waves with custom colors and line styles.

x = linspace(0,7); y = ones(length(x),9);fori = 1:9 y(:,i) = sin(x-i/5)';endplot(x,y) colororder({的d','green','blue'}) ax = gca; ax.LineStyleOrder = {“- - -”,'--',':'};

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

Return the solid red line. Then, change the thickness of the line.

h = findobj('Color',的d','LineStyle',“- - -”)
h = Line with properties: Color: [1 0 0] LineStyle: '-' LineWidth: 0.5000 Marker: 'none' MarkerSize: 6 MarkerFaceColor: 'none' XData: [0 0.0707 0.1414 0.2121 0.2828 0.3535 0.4242 0.4949 ... ] YData: [-0.1987 -0.1289 -0.0586 0.0121 0.0827 0.1529 ... ] Show all properties
h.LineWidth = 2;

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

Create a multiline plot. Specify an identifier for each plot.

x = linspace(-1,1); y1 = x; plot(x,y1,'Tag','linear') holdony2 = x.^2; plot(x,y2,'Tag','quadratic') y3 = exp(x); plot(x,y3,'Tag','exponential') y4 = sin(x); plot(x,y4,'Tag','sinusoidal') holdoff

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

Find all objects whoseTag属性没有设置'linear'.

h1 = findobj('-not','Tag','linear')
h1 = 6x1 graphics array: Root Figure (1) Axes Line (sinusoidal) Line (exponential) Line (quadratic)

Find all objects whoseTag属性没有设置'linear'or'quadratic'.

h2 = findobj('-not',{'Tag','linear','-or','Tag','quadratic'})
h2 = 5x1 graphics array: Root Figure (1) Axes Line (sinusoidal) Line (exponential)

Find all line objects whoseTag属性没有设置'linear'or'quadratic'.

h3 = findobj('Type','line','-not',{'Tag','linear','-or','Tag','quadratic'})
h3 = 2x1 Line array: Line (sinusoidal) Line (exponential)

Improve the readability of the previous statement by using'-and'and curly brackets.

h4 = findobj({'Type','line'},'-and',{'-not',{'Tag','linear','-or','Tag','quadratic'}})
h4 = 2x1 Line array: Line (sinusoidal) Line (exponential)

Create three line plots and assign an identifier to two of the plots.

x = linspace(-1,1); y1 = x; plot(x,y1) holdony2 = x.^2; plot(x,y2,'Tag','Quadratic') y3 = exp(x); plot(x,y3,'Tag','Exponential') holdoff

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

Find all objects that have a nonemptyTagproperty.

h = findobj('-regexp','Tag','[^'']')
h = 2x1 Line array: Line (Exponential) Line (Quadratic)

Create a vector of four values. Display the values using a line plot, an area plot, and a bar graph.

y = [1 5 6 3]; subplot(3,1,1) plot(y) subplot(3,1,2) area(y) subplot(3,1,3) bar(y)

Figure contains 3 axes objects. Axes object 1 contains an object of type line. Axes object 2 contains an object of type area. Axes object 3 contains an object of type bar.

Return all objects that have aBaseValueproperty.

h = findobj('-property','BaseValue')
h = 2x1 graphics array: Bar Area

Create a plot of random values, and then return all line objects in the current axes.

plot(rand(5))

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

h = findobj(gca,'Type','line')
h = 5x1 Line array: Line Line Line Line Line

Usehto query theyvalues of the firstLineobject.

values = h(1).YData
values =1×50.6557 0.0357 0.8491 0.9340 0.6787

Create a figure with two tabs. Add axes to each tab by specifying the parent container for each one. Plot a line in the first tab and a surface in the second tab.

figure tab1 = uitab('Title','Tab1'); ax1 = axes(tab1); plot(ax1,1:10) tab2 = uitab('Title','Tab2'); ax2 = axes(tab2); surf(ax2,peaks)

Figure contains 2 axes objects and another object of type uitabgroup. Axes object 1 contains an object of type surface. Axes object 2 contains an object of type line.

Return all objects in the current figure and its descendants.

h = findobj(gcf)
h = 8 x1图形阵列:图(1)TabGroup选项卡(Tab1) Tab (Tab2) Axes Axes Line Surface

Create a figure with two stacked subplots.

subplot(2,1,1) x = linspace(0,10); y1 = sin(x); plot(x,y1) subplot(2,1,2) y2 = sin(5*x); plot(x,y2)

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

Find all objects in the current figure and its children.

h1 = findobj(gcf,'-depth',1)
h1 = 3x1 graphics array: Figure (1) Axes Axes

Find all objects in the current figure and any descendants that are up to two levels lower in the graphics object hierarchy.

h2 = findobj(gcf,'-depth',2)
h2 = 5x1 graphics array: Figure (1) Axes Axes Line Line

Restrict the search to the current figure and the current axes using the'flat'option.

h3 = findobj([gcf,gca],'flat')
h3 = 2x1 graphics array: Figure (1) Axes

Input Arguments

collapse all

Property name, specified as a character vector or string scalar. For more information, see图形对象属性.

Example:'Tag'

Example:'Type'

Property value, specified as a scalar or array.

Logical operator, specified as'-and','-or', or'-xor'. Logical operator precedence follows MATLAB®precedence rules. For more information, seeOperator Precedence.

To control operator precedence, groupprop,valuepairs within cell arrays. For example, find all objects that have aTagproperty set to'button one'and aColorproperty set to a value other than的d'or'blue':

h = findobj('Tag','button one','-and',...'-not',{'Color',的d','-or','Color','blue'})

Regular expression, specified as a string array, character vector, or cell array of character vectors.exprcan contain characters, metacharacters, operators, tokens, and flags that specify patterns to match in the property value. You can useexpronly when the property value is a string or character vector. For more information about regular expressions, seeregexp.

Objects to search from, specified as an array of graphics objects. Unless you specify the'-depth'or'flat'options,findobjsearches the objects in the input arrayobjhandlesand all of their descendants in the graphics object hierarchy.

深度搜索, specified as a nonnegative integer indicating the number of levels below any given object in the input arrayobjhandles.

  • d = n— Searchnlevels of the hierarchy below each object inobjhandles.

  • d = 0— Search only the same level as the objects inobjhandles. This is equivalent to specifying the'flat'option.

  • d = inf— Search all levels below the objects inobjhandles. This is equivalent to a default search without specifying the'-depth'or'flat'options.

Tips

  • If theHandleVisibilityproperty of an object is set to'off',findobjdoes not return that graphics object or any of its descendants. To return all objects in the hierarchy, including hidden objects, use thefindallfunction.

  • findobjcorrectly matches any legal property value. For example, this code finds all objects having aColorproperty set tored,r, or[1 0 0]:

    findobj('Color','r')

  • When a graphics object is a descendant of more than one object identified inobjhandles, MATLAB searches the object each timefindobjencounters its handle. Therefore, implicit references to a graphics object can result in multiple returns of the object.

Version History

之前介绍过的R2006a