Finding Objects with Specific Values

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

Assume three of the PhoneBook entries in the database are:

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

ans =

1M

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

ans =

5081234568

Find by Attribute Settings

All metaclasses derive from the handle class so 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 the meta.class MethodList for meta.method objects with their Abstract property set to true:

% Use class name in string form because class is abstract
mc = meta.class.fromName('ClassName'); 
% Search list of meta.method objects for those
% methods that have their Abstract 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:

  • Get the meta.class object

  • Use findobj to search the array of meta.property objects

mc = ?containers.Map;
% findobj returns an array of meta.property objects
% use braces to convert the comman separated list to a cell array
mpArray = findobj(mc.PropertyList,'GetAccess','public');
% create cell array of property names
names = {mpArray.Name};

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

celldisp(names)
 
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))

ans =

     0

findobj returns an array of meta.method objects for the static methods. In this case, isempty returns false, indicating there are static methods defined by this class.

You can get the names of any static methods from the meta.method array:

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

staticMethodInfo(:).Name

ans =

empty

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

staticMethodInfo

  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 meta.class]
Was this topic helpful?