Find Objects with Specific Values - MATLAB & Simulink (original) (raw)

Find Handle Objects

Use the handle class findobj method to find objects that have properties with specific values. For example, the following class defines a PhoneBook object to represent a telephone book entry in a data base. The PhoneBook class subclasses the dynamicprops class, which derives from handle.

classdef PhoneBook < dynamicprops properties Name Address Number end methods function obj = PhoneBook(n,a,p) obj.Name = n; obj.Address = a; obj.Number = p; end end end

Here are three of the PhoneBook entries in the database:

PB(1) = PhoneBook('Nancy Vidal','123 Washington Street','5081234567'); PB(2) = PhoneBook('Nancy Vidal','123 Main Street','5081234568'); PB(3) = PhoneBook('Nancy Wong','123 South Street','5081234569');

One of these three PhoneBook objects has a dynamic property:

PB(2).addprop('HighSpeedInternet'); PB(2).HighSpeedInternet = '1M';

Find Property/Value Pairs

Find the object representing employee Nancy Wong and display the name and number by concatenating the strings:

NW = findobj(PB,'Name','Nancy Wong'); [NW.Name,' - ',NW.Number]

ans =

Nancy Wong - 5081234569

Find Objects with Specific Property Names

Search for objects with specific property names using the -property option:

H = findobj(PB,'-property','HighSpeedInternet'); H.HighSpeedInternet

The -property option enables you to omit the value of the property and search for objects using only the property name.

Using Logical Expressions

Search for specific combinations of property names and values:

H = findobj(PB,'Name','Nancy Vidal','-and','Address','123 Main Street'); H.Number

Find by Attribute Settings

All metaclasses derive from the handle class. You can use the handle findobj method to find class members that have specific attribute settings.

For example, find the abstract methods in a class definition by searching thematlab.metadata.Class MethodList for matlab.metadata.Method objects with their Abstract property set to true:

Use the class name in character format because class is abstract. You cannot create an object of the class:

mc = matlab.metadata.Class.fromName('MyClass');

Search the MethodList list of matlab.metadata.Method objects for those methods that have theirAbstract property set to true:

absMethods = findobj(mc.MethodList,'Abstract',true); methodNames = {absMethods.Name};

The cell array, methodNames, contains the names of the abstract methods in the class.

Find Properties That Have Public Get Access

Find the names of all properties in the containers.Map class that have public GetAccess:

mc = ?containers.Map; mpArray = findobj(mc.PropertyList,'GetAccess','public'); names = {mpArray.Name};

Display the names of all containers.Map properties that have public GetAccess:

names{1} =

Count

names{2} =

KeyType

names{3} =

ValueType

Find Static Methods

Determine if any containers.Map class methods are static:

~isempty(findobj([mc.MethodList(:)],'Static',true))

findobj returns an array of matlab.metadata.Method objects for the static methods. In this case, the list of static methods is not empty. Therefore, there are static methods defined by this class.

Get the names of any static methods from the matlab.metadata.Method array:

staticMethodInfo = findobj([mc.MethodList(:)],'Static',true); staticMethodInfo(:).Name

The name of the static method (there is only one in this case) is empty. Here is the information from the matlab.metadata.Method object for the empty method:

method with properties:

               Name: 'empty'
        Description: 'Returns an empty object array of the given size'
DetailedDescription: ''
             Access: 'public'
             Static: 1
           Abstract: 0
             Sealed: 0
             Hidden: 1
         InputNames: {'varargin'}
        OutputNames: {'E'}
      DefiningClass: [1x1 matlab.metadata.Class]

See Also

empty