Documentation

Inspect Class and Object Metadata

Inspect a 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 text string')
         end
      end
   end
end

Inspect Class Definition

Using the EmployeeData class, create a meta.class object using the ? operator:

mc = ?EmployeeData;

Determine from what classes EmployeeData derives. The returned value is a meta.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 meta.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 meta.properties objects from the meta.class PropertyList property.

mc = ?EmployeeData;
mpArray = mc.PropertyList;

The length of mpArray indicates there are two meta.property objects, one for each property defined by the EmployeeData class:

length(mpArray)
ans =
     2

Now get a meta.property object from the array:

prop1 = mpArray(1);
prop1.Name
ans =

EmployeeName 

The Name property of the meta.property object identifies the class property represented by that meta.property object.

Query other meta.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
ans =

EmployeeNumber

Note that Access is not a property of the meta.property class. You must use SetAccess and GetAccess, which are properties of the meta.property class.

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

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
ans =
   My Name
EdObj.(mpArray(2).Name)
You cannot get the 'EmployeeNumber' property of EmployeeData.
mpArray(2).GetAccess
ans =
   private

Obtain a function handle to the property set access function:

mpArray(1).SetMethod
ans = 
   @D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName

Metaclass EnumeratedValues Property

The meta.class EnumeratedValues property contains an array of meta.EnumeratedValue objects, one for each enumeration member. Use the meta.EnumeratedValue 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 meta.class object:

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

Tuesday

Related Examples

Was this topic helpful?