Main Content

Get Information About Properties

The meta.property Object

Use the meta.property class to determine the values 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 values specified in the class definition.

You can get the meta.property object for a property from the meta.class object. To get the meta.class object for a class:

  • Use the metaclass function on an object of the class.

  • Use the ? operator with the class name.

For example, the BasicHandle class defines three properties:

classdef BasicHandle < handle
   % BasicHandle  Inherits from handle superclass
   % Defines 1 public and 2 private properties.
   properties (SetAccess = private)
      Date = date
      PassKey = randi(9,[1,7]) 
   end
   properties
      Category {mustBeMember(Category,{'new','change'})} = 'new'
   end
end

Create the meta.class object using the ? operator with the class name:

mc = ?BasicHandle
mc = 

  class with properties:

                    Name: 'BasicHandle'
             Description: 'BasicHandle  Inherits from handle superclass'
     DetailedDescription: '  Defines 1 public and 2 private properties.'
                  Hidden: 0
                  Sealed: 0
                Abstract: 0
             Enumeration: 0
         ConstructOnLoad: 0
        HandleCompatible: 1
         InferiorClasses: {0×1 cell}
       ContainingPackage: [0×0 meta.package]
                 Aliases: [0×1 string]
    RestrictsSubclassing: 0
            PropertyList: [3×1 meta.property]
              MethodList: [24×1 meta.method]
               EventList: [1×1 meta.event]
   EnumerationMemberList: [0×1 meta.EnumeratedValue]
          SuperclassList: [1×1 meta.class]

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

mc.PropertyList(1).Name
ans =

Date

The meta.class object contains a meta.property object for all properties, including hidden properties. The properties function returns only public properties.

For a handle class, use the handle findprop method to get the meta.property object for a specific property.

For example, find the meta.property object for the Category property of the BasicHandle class.

h = BasicHandle;
mp = findprop(h,'Category')
mp = 

  property with properties:

                   Name: 'Category'
            Description: ''
    DetailedDescription: ''
              GetAccess: 'public'
              SetAccess: 'public'
              Dependent: 0
               Constant: 0
               Abstract: 0
              Transient: 0
                 Hidden: 0
          GetObservable: 0
          SetObservable: 0
               AbortSet: 0
            NonCopyable: 0
              GetMethod: []
              SetMethod: []
             HasDefault: 1
           DefaultValue: 'new'
          DefiningClass: [1×1 meta.class]

The meta.property display shows that a default BasicHandle object Category property:

  • Has public GetAccess and SetAccess

  • Has a default value of new

For a list of property attributes, see Table of Property Attributes.

How to Index 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 char vector 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, 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 letters (C and t) of the char vector 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 values. For example, you can:

  • Find objects that define constant properties (Constant attribute set to true).

  • 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. The function accesses information from metadata using these techniques:

  • If input argument, obj, is a char vector, 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)
   if ischar(obj)
      mc = meta.class.fromName(obj);
   elseif isobject(obj)
      mc = metaclass(obj);
   end
   ii = 0; numb_props = length(mc.PropertyList);
   cl_array = cell(1,numb_props);
   for  c = 1:numb_props
      mp = mc.PropertyList(c);
      if isempty (findprop(mp,attrName))
         error('Not a valid attribute name')
      end
      attrValue = mp.(attrName);
      if attrValue
         if islogical(attrValue) || strcmp(varargin{1},attrValue)
            ii = ii + 1;
            cl_array(ii) = {mp.Name};
         end
      end
   end
   cl_out = cl_array(1:ii);
end

Find Property Attributes

Define a 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'

Related Topics