Class Introspection with Metadata
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 a
matlab.metadata.Class object using the ?
operator:
mc = ?EmployeeData;
Determine from what classes EmployeeData derives. The returned value is a
matlab.metadata.Class object for the handle
superclass:
a = mc.SuperclassList; a.Name
ans = handle
The EmployeeData class has only one superclass. For classes having more
than one direct superclass, a contains a
matlab.metadata.Class object for each superclass.
Use an indexed reference to refer to any particular superclass:
a(1).Name
or, directly from mc:
mc.SuperclassList(1).Name
ans = handle
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 the
matlab.metadata.Class
PropertyList property.
mc = ?EmployeeData; mpArray = mc.PropertyList;
The length of mpArray indicates that there are two
matlab.metadata.Properties objects, one for each property
defined by the EmployeeData class:
length(mpArray)
ans =
2Now get a matlab.metadata.Property object from the array:
prop1 = mpArray(1); prop1.Name
ans = EmployeeName
The Name property of the matlab.metadata.Property object
identifies the class property represented by that
matlab.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')).Nameans = EmployeeNumber
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
ans = empty
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.
ans = My Name
The value of the EmployeeNumber property
is not accessible because the property has private Access.
EdObj.(mpArray(2).Name)
You cannot get the 'EmployeeNumber' property of EmployeeData.
mpArray(2).GetAccess
ans = private
Obtain a function handle to the EmployeeName property
set access function:
mpArray(1).SetMethod
ans = @D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName
Metaclass EnumeratedValues Property
The matlab.metadata.Class
EnumerationMemberList property contains an array of
matlab.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
ans = Tuesday