Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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 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 that 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

Access is not a property of the meta.property class. 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 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 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?