Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Finding Objects Having Specific Settings

Finding 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';

Finding a Property/Value Pairs

Find the object representing employee Nancy Wong:

NW = findobj(PB,'Name','Nancy Wong');

[NW.Name ' - ' NW.Number] 

ans =

Nancy Wong - 5081234569

Finding 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

Perusing Meta-Classes with findobj

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

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

The cell array names contains the names of all containers.Map properties that have public GetAccess:

celldisp(names)
 
names{1} =
 
Count
 
 names{2} =
 
KeyType
 
names{3} =
 
ValueType

Determine if any containers.Map class methods are static:

isempty(findobj([mc.Methods{:}],'Static',true))

ans =

     1

findobj returns an empty meta.property object indicating that there are no static methods.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS