Getting Information About Properties

The meta.property object

The meta.property class is useful for determining the settings of property attributes. The writable properties of a meta.property object correspond to the attributes of the associated property. The values of the writable meta.property properties correspond to the attribute setting specified in the class definition.

For example, create a default containers.Map object and use the handle findprop method to get the meta.property object for the Count property:

mp = findprop(containers.Map,'Count')

mp = 

  property with properties:

                   Name: 'Count'
            Description: 'Number of pairs in the collection'
    DetailedDescription: ''
              GetAccess: 'public'
              SetAccess: 'private'
              Dependent: 1
               Constant: 0
               Abstract: 0
              Transient: 1
                 Hidden: 0
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
              GetMethod: []
              SetMethod: []
          DefiningClass: [1x1 meta.class]

The preceding meta.property display shows that the default Map object Count property has public GetAccess and private SetAccess, is Dependent, and Transient. See Table of Property Attributes for a list of property attributes.

If you are working with a class that is not a handle class, get the meta.property objects from the meta.class object. All metaclasses are subclasses of the handle class. Use the metaclass function if you have an instance or the ? operator with the class name:

mc = ?containers.Map

mc = 

  class with properties:

                     Name: 'containers.Map'
              Description: 'MATLAB Map Container'
      DetailedDescription: 'MATLAB Map Container'
                   Hidden: 0
                   Sealed: 0
          ConstructOnLoad: 1
         HandleCompatible: 1
          InferiorClasses: {0x1 cell}
        ContainingPackage: [1x1 meta.package]
             PropertyList: [4x1 meta.property]
               MethodList: [35x1 meta.method]
                EventList: [1x1 meta.event]
    EnumerationMemberList: [0x1 meta.EnumeratedValue]
           SuperclassList: [1x1 meta.class]

The meta.class object property named PropertyList contains an array of meta.property objects, one for each property defined by the containers.Map class. For example, the name of the property associated with the meta.property object in element 1 is:

mc.PropertyList(1).Name

ans =

Count

The meta.class object contains a meta.property object for hidden properties too. Compare the result with the properties function, which returns only public properties:

properties('containers.Map')

Properties for class containers.Map:

    Count
    KeyType
    ValueType

The serialization property is Hidden and has its GetAccess and SetAccess attributes set to private. Therefore, the properties function does not list it. However, you can get information about this property from its associated meta.property object (which is the fourth element in the array of meta.property objects in this case):

mc.PropertyList(4)

ans = 

  class with properties:

                   Name: 'serialization'
            Description: 'Serialization property.'
    DetailedDescription: ''
              GetAccess: 'private'
              SetAccess: 'private'
              Dependent: 0
               Constant: 0
               Abstract: 0
              Transient: 0
                 Hidden: 1
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
              GetMethod: []
              SetMethod: []
          DefiningClass: [1x1 meta.class]

Indexing Metaclass Objects

Access other metaclass objects directly from the meta.class object properties. For example, the statement:

mc = ?containers.Map;

returns a meta.class object:

class(mc)

ans =

meta.class

Referencing the PropertyList meta.class property returns an array with one meta.property object for each property of the containers.Map class:

class(mc.PropertyList)

ans =

meta.property

Each array element is a single meta.property object:

mc.Properties(1)

ans = 

    [1x1 meta.property]

The Name property of the meta.property object contains a character string that is the name of the property:

class(mc.PropertyList(1).Name)

ans =

char

Apply standard MATLAB® indexing to access information in metaclass objects.

For example, because the meta.class PropertyList property contains an array of meta.property objects, the following expression accesses the first meta.property object in this array and returns the first and last (C and t) letters of the string contained in the meta.property Name property.

mc.PropertyList(1).Name([1 end])

ans =

Ct

How to Find Properties with Specific Attributes

This example implements a function that finds properties with specific attribute settings. For example, find objects that define constant properties (Constant attribute set to true) or determine what properties are read-only (GetAccess = public, SetAccess = private). The findAttrValue function returns a cell array of property names that set the specified attribute.

This function accesses information from metaclasses using these techniques:

  • If input argument, obj, is a string, use the meta.class.fromName static method to get the meta.class object.

  • If input argument, obj, is an object, use the metaclass function to get the meta.class object.

  • Every property has an associated meta.property object. Obtain these objects from the meta.class PropertyList property.

  • Use the handle class findprop method to determine if the requested property attribute is a valid attribute name. All property attributes are properties of the meta.property object. The statement, findobj(mp,'PropertyName') determines whether the meta.property object, mp, has a property called PropertyName.

  • Reference meta.property object properties using dynamic field names. For example, if attrName = 'Constant', then MATLAB converts the expression mp.(attrName) to mp.Constant

  • The optional third argument enables you to specify the value of attributes whose values are not logical true or false (such as GetAccess and SetAccess).

function cl_out = findAttrValue(obj,attrName,varargin)

   % Determine if first input is object or class name
   if ischar(obj)
      mc = meta.class.fromName(obj);
   elseif isobject(obj)
      mc = metaclass(obj);
   end

   % Initialize and preallocate
   ii = 0; numb_props = length(mc.PropertyList);
   cl_array = cell(1,numb_props);

   % For each property, check the value of the queried attribute
   for  c = 1:numb_props

      % Get a meta.property object from the meta.class object
      mp = mc.PropertyList(c); 

      % Determine if the specified attribute is valid on this object
      if isempty (findprop(mp,attrName))
         error('Not a valid attribute name')
      end
      attrValue = mp.(attrName);

      % If the attribute is set or has the specified value,
      % save its name in cell array
      if attrValue
         if islogical(attrValue) || strcmp(varargin{1},attrValue)
            ii = ii + 1;
            cl_array(ii) = {mp.Name}; 
         end
      end
   end
   % Return used portion of array
   cl_out = cl_array(1:ii);
end

Find Property Attributes

Suppose you have the following containers.Map object:

mapobj = containers.Map({'rose','bicycle'},{'flower','machine'});

Find properties with private SetAccess:

findAttrValue(mapobj,'SetAccess','private')

ans = 

    'Count'    'KeyType'    'ValueType'    'serialization'

Find properties with public GetAccess:

findAttrValue(mapobj,'GetAccess','public')

ans = 

    'Count'    'KeyType'    'ValueType'
Was this topic helpful?