Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Using Meta-Classes to Inspect Classes and Objects

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.SuperClasses; % a is cell array of meta.class objects
a{1}.Name
ans =
handle

Inspecting Properties

Find the names of the properties defined by this class. First obtain a cell array of meta.properties objects from the meta.class Properties property.

mpCell = mc.Properties;

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

length(mpCell)
ans =
     2

Now get a meta.property object from the cell array:

prop1 = mpCell{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 EmployName property.

Inspecting an Instance of a Class

Create a EmployeeData object and determine property access settings:

EdObj = EmployeeData('My Name',1234567);
mcEdObj = metaclass(EdObj);
mpCell = mcEdObj.Properties;
EdObj.(mpCell{1}.Name) % Dynamic field names work with objects
ans =
   My Name
EdObj.(mpCell{2}.Name)
??? Getting the 'EmployeeNumber' property of the 'EmployeeData' class is
not allowed.
mpCell{2}.GetAccess
ans =
   private

Obtain a function handle to the property set access function:

mpCell{1}.SetMethod
ans = 
   @D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS