Main Content

Get Information About Properties

The matlab.metadata.Property Object

Use the matlab.metadata.Property class to determine the values of property attributes. The writable properties of a matlab.metadata.Property object correspond to the attributes of the associated property. The values of the writable matlab.metadata.Property properties correspond to the attribute values specified in the class definition.

You can get the matlab.metadata.Property object for a property from the matlab.metadata.Class object. To get the matlab.metadata.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 one public and two private 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 matlab.metadata.Class object using the ? operator with the class name:

mc = ?BasicHandle
mc = 

  Class with properties:

                    Name: 'BasicHandle'
             Description: '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 matlab.metadata.Class]
               Namespace: [0×0 matlab.metadata.Namespace]
                 Aliases: [0×1 string]
    RestrictsSubclassing: 0
            PropertyList: [3×1 matlab.metadata.Property]
              MethodList: [24×1 matlab.metadata.Method]
               EventList: [1×1 matlab.metadata.Event]
   EnumerationMemberList: [0×1 matlab.metadata.EnumerationMember]
          SuperclassList: [1×1 matlab.metadata.Class]

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

mc.PropertyList(1).Name
ans =

Date

The matlab.metadata.Class object contains a matlab.metadata.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 matlab.metadata.Property object for a specific property.

For example, find the matlab.metadata.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
    PartialMatchPriority: 1
               GetMethod: []
               SetMethod: []
              HasDefault: 1
            DefaultValue: 'new'
              Validation: [1×1 matlab.metadata.Validation]
           DefiningClass: [1×1 matlab.metadata.Class]

The matlab.metadata.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 matlab.metadata.Class object properties. For example, the statement:

mc = ?containers.Map;

returns a matlab.metadata.Class object:

class(mc)
ans =

matlab.metadata.Class

Referencing the PropertyList matlab.metadata.Class property returns an array with one matlab.metadata.Property object for each property of the containers.Map class:

class(mc.PropertyList)
ans =

matlab.metadata.Property

Each array element is a single matlab.metadata.Property object:

mc.Properties(1)
ans = 

    [1x1 matlab.metadata.Property]

The Name property of the matlab.metadata.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 matlab.metadata.Class PropertyList property contains an array of matlab.metadata.Property objects. The following expression accesses the first matlab.metadata.Property object in this array and returns the first and last letters (C and t) of the char vector contained in the matlab.metadata.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 matlab.metadata.Class.fromName static method to get the matlab.metadata.Class object.

  • If input argument, obj, is an object, use the metaclass function to get the matlab.metadata.Class object.

  • Every property has an associated matlab.metadata.Property object. Obtain these objects from the matlab.metadata.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 matlab.metadata.Property object. The statement, findobj(mp,'PropertyName') determines whether the matlab.metadata.Property object, mp, has a property called PropertyName.

  • Reference matlab.metadata.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 = matlab.metadata.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