Inspecting Class and Object Metadata

Inspecting 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

Inspecting the Class Definition

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

mc = ?EmployeeData;

Determine from what classes EmployeeData derives:

a = mc.SuperclassList; % a is an array of meta.class objects
a.Name

ans =

handle

The EmployeeData class has only one superclass. For classes having more than one superclass, a would contain 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

Inspecting 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.

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.

Inspecting an Instance of a Class

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)
Getting the 'EmployeeNumber' property of the 'EmployeeData' class is not allowed.
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
Was this topic helpful?