Main Content

Get Information About Properties

The Object

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

You can get the 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]) 
      Category {mustBeMember(Category,{'new','change'})} = 'new'

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]
              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 objects, one for each property defined by the 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 all properties, including hidden properties. The properties function returns only public properties.

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

For example, find the 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 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:

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 char vector that is the name of the property:

ans =


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

For example, 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 letters (C and t) of the char vector 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 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 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)
   if ischar(obj)
      mc = meta.class.fromName(obj);
   elseif isobject(obj)
      mc = metaclass(obj);
   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')
      attrValue = mp.(attrName);
      if attrValue
         if islogical(attrValue) || strcmp(varargin{1},attrValue)
            ii = ii + 1;
            cl_array(ii) = {mp.Name};
   cl_out = cl_array(1:ii);

Find Property Attributes

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

Related Topics