Get Information About Properties - MATLAB & Simulink (original) (raw)

Main Content

The matlab.metadata.Property Object

Use the matlab.metadata.Property class to determine the values of property attributes. The writable properties of amatlab.metadata.Property object correspond to the attributes of the associated property. The values of the writablematlab.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 thematlab.metadata.Class object for a class:

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 =

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 namedPropertyList contains an array ofmatlab.metadata.Property objects, one for each property defined by the class. For example, the name of the property associated with thematlab.metadata.Property object in element 1 is:

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

For example, find the matlab.metadata.Property object for theCategory 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 defaultBasicHandle object Category property:

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

How to Index Metaclass Objects

Access other metaclass objects directly from thematlab.metadata.Class object properties. For example, the statement:

returns a matlab.metadata.Class object:

ans =

matlab.metadata.Class

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

ans =

matlab.metadata.Property

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

ans =

[1x1 matlab.metadata.Property]

The Name property of thematlab.metadata.Property object contains achar vector that is the name of the property:

class(mc.PropertyList(1).Name)

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

For example, the matlab.metadata.Class PropertyList property contains an array ofmatlab.metadata.Property objects. The following expression accesses the first matlab.metadata.Property object in this array and returns the first and last letters (C andt) of the char vector contained in thematlab.metadata.Property Name property.

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

How to Find Properties with Specific Attributes

This example implements a function that finds properties with specific attribute values. For example, you can:

The findAttrValue function returns a cell array of property names that set the specified attribute. The function accesses information from metadata using these techniques:

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'

See Also

Topics