Class Introspection with Metadata - MATLAB & Simulink (original) (raw)

Using Class Metadata

Use class metadata to get information about classes and objects programmatically. For example, you can determine attribute values for class members or get a list of events defined by the class. For basic information about metadata, see Class Metadata.

Inspect the EmployeeData Class

The EmployeeData class is a handle class with two properties, one of which has private Access and defines a set access method.

classdef EmployeeData < handle properties EmployeeName end properties (Access = private) EmployeeNumber end methods function obj = EmployeeData(name,ss) if nargin > 0 obj.EmployeeName = name; obj.EmployeeNumber = ss; end end function set.EmployeeName(obj,name) if ischar(name) obj.EmployeeName = name; else error('Employee name must be a char vector') end end end end

Inspect Class Definition

Using the EmployeeData class, create amatlab.metadata.Class object using the ? operator:

Determine from what classes EmployeeData derives. The returned value is amatlab.metadata.Class object for the handle superclass:

a = mc.SuperclassList; a.Name

The EmployeeData class has only one superclass. For classes having more than one direct superclass, a contains amatlab.metadata.Class object for each superclass.

Use an indexed reference to refer to any particular superclass:

or, directly from mc:

mc.SuperclassList(1).Name

The SuperclassList property contains only direct superclasses.

Inspect Properties

Find the names of the properties defined by the EmployeeData class. First obtain an array of matlab.metadata.Property objects from thematlab.metadata.Class PropertyList property.

mc = ?EmployeeData; mpArray = mc.PropertyList;

The length of mpArray indicates that there are twomatlab.metadata.Properties objects, one for each property defined by the EmployeeData class:

Now get a matlab.metadata.Property object from the array:

prop1 = mpArray(1); prop1.Name

The Name property of the matlab.metadata.Property object identifies the class property represented by thatmatlab.metadata.Property object.

Query other matlab.metadata.Property object properties to determine the attributes of the EmployeeName properties.

Find Component with Specific Attribute

You can use indexing techniques to list class components that have specific attribute values. For example, this code lists the methods in the EmployeeData class that have private access:

mc = ?EmployeeData; mc.PropertyList(ismember({mc.PropertyList(:).SetAccess},'private')).Name

Access is not a property of the matlab.metadata.Property class. Use SetAccess and GetAccess, which are properties of the matlab.metadata.Property class.

Find components with attributes that are logical values using a statement like this one:

mc = ?handle; mc.MethodList(ismember([mc.MethodList(:).Hidden],true)).Name

Inspect Class Instance

Create an EmployeeData object and determine property access settings:

EdObj = EmployeeData('My Name',1234567); mcEdObj = metaclass(EdObj); mpArray = mcEdObj.PropertyList; EdObj.(mpArray(1).Name) % Dynamic field names work with objects

The value of the EmployeeName property is the text My Name, which was assigned in the constructor.

The value of the EmployeeNumber property is not accessible because the property has private Access.

You cannot get the 'EmployeeNumber' property of EmployeeData.

Obtain a function handle to the EmployeeName property set access function:

ans = @D:\MyDir@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName

Metaclass EnumeratedValues Property

The matlab.metadata.Class EnumerationMemberList property contains an array ofmatlab.metadata.EnumerationMember objects, one for each enumeration member. Use the matlab.metadata.EnumerationMember Name property to obtain the enumeration member names defined by an enumeration class. For example, given the WeekDays enumeration class:

classdef WeekDays enumeration Monday, Tuesday, Wednesday, Thursday, Friday end end

Query enumeration names from the matlab.metadata.Class object:

mc = ?WeekDays; mc.EnumerationMemberList(2).Name

See Also

Topics