Getting Information About Properties

The object

The class is useful for determining the settings of property attributes. The writable properties of a object correspond to the attributes of the associated property. The values of the writable 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 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 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 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]
               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 objects, one for each property defined by the containers.Map class. For example, the name of the property associated with the object in element 1 is:


ans =


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


Properties for class containers.Map:


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 object (which is the fourth element in the array of objects in this case):


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:


ans =


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


ans =

Each array element is a single object:


ans = 


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


ans =


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

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

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

ans =


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 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 object. The statement, findobj(mp,'PropertyName') determines whether the object, mp, has a property called PropertyName.

  • Reference 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);

   % 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 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')
      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}; 
   % Return used portion of array
   cl_out = cl_array(1:ii);

Find Property Attributes

Suppose you have the following containers.Map object:

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

Find properties with private SetAccess:


ans = 

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

Find properties with public GetAccess:


ans = 

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