matlab.metadata.Class - Describe MATLAB class - MATLAB (original) (raw)
Namespace: matlab.metadata
Superclasses: matlab.metadata.MetaData
Describe MATLAB class
Renamed from meta.class
in R2024a
Description
The matlab.metadata.Class
class provides a way to get descriptive information about MATLAB® classes. By creating a matlab.metadata.Class
object for a specific class, you can get information about the class definition.
matlab.metadata.Class
properties contain lists of properties, methods, events, and class attributes set in the classdef
line, as well as other information about how the class is defined.
You cannot set the values of matlab.metadata.Class
object properties. You can only query the properties.
Create a matlab.metadata.Class
object from an instance of a class or using the class name using these options:
metaclass(obj)
returns amatlab.metadata.Class
object representing the object passed as an argument.?
ClassName
returns amatlab.metadata.Class
object representing the named class.- matlab.metadata.Class.fromName
("_`ClassName`_")
is a static method that returns amatlab.metadata.Class
object representing the named class.
You cannot instantiate a matlab.metadata.Class
object directly by calling its constructor.
The matlab.metadata.Class
class is a handle class.
Class Attributes
Abstract | true |
---|---|
HandleCompatible | true |
For information on class attributes, see Class Attributes.
Creation
Create a matlab.metadata.Class
object from an instance of a class or using the class name using these options:
metaclass(obj)
returns amatlab.metadata.Class
object representing the object passed as an argument.?
ClassName
returns amatlab.metadata.Class
object representing the named class.- matlab.metadata.Class.fromName
("_`ClassName`_")
is a static method that returns amatlab.metadata.Class
object representing the named class.
You cannot instantiate a matlab.metadata.Class
object directly.
Properties
Name of the class, returned as a character vector. The name returned by this property includes the class namespace.
Short description of the class, returned as a character vector. For user-defined classes, the text for this property comes from comments in the class definition. If there are no comments, the property returns an empty character vector. For more information on how to include help text for your classes, see Custom Help Text.
Detailed description of the class, returned as a character vector. For user-defined classes, the text for this property comes from comments in the class definition. If there are no comments, the property returns an empty character vector. For more information on how to include help text for your classes, see Custom Help Text.
Is class hidden from inspection tools, returned as logical true
or false
. If true
, the class does not appear in the output of MATLAB commands or tools that display class names. However, you can access the class programmatically.
Data Types: logical
Value of class attribute Sealed
, returned as a logical value. IfSealed
is true
, this class cannot be subclassed.
Data Types: logical
Value of class attribute Abstract
, returned as a logical value. The value of this property is true
if the class or any property or method has its Abstract
attribute set to true
. For information on abstract classes, see Abstract Classes and Class Members.
Data Types: logical
Is class an enumeration class, returned as a logical value. Iftrue
, this class is an enumeration class. For more information about enumeration classes, see Define Enumeration Classes.
Data Types: logical
Value of class attribute ConstructOnLoad
, returned as a logical value. If true
, MATLAB calls the class constructor automatically when loading an object from a MAT-file. To enable ConstructOnLoad
, the constructor must support being called with no input arguments. For more information, see Default Save and Load Process for Objects.
Data Types: logical
Value of class attribute HandleCompatible
, returned as a logical value. If true
, this class is a handle-compatible class. For more information about handle-compatible classes, see Handle Compatible Classes.
Value of class attribute InferiorClasses
, returned as amatlab.metadata.Class
object array. For information on class precedence, see Class Precedence.
Namespace containing the class, returned as amatlab.metadata.Namespace
object. If the class is not in a namespace, this property contains an empty matlab.metadata.Namespace
object. For more information about namespaces, see Create Namespaces.
List of aliases defined for the class, returned as a string array. The aliases are listed from oldest to newest.
Does class restrict subclassing, returned as a logical true
orfalse
. MATLAB sets this property to true
when the class restricts subclassing by:
- Setting the
Sealed
attribute totrue
- Specifying the classes that can subclass this class using the
AllowedSubclasses
attribute
For more information restricting subclassing, see Specify Allowed Subclasses.
Properties defined for the class, returned as an array ofmatlab.metadata.Property
objects. Thematlab.metadata.Property
objects describe each property defined by this class, including all inherited public and protected properties. For more information on properties, see Properties.
Methods defined for the class, returned as an array ofmatlab.metadata.Method
objects. Thematlab.metadata.Method
objects describe each method defined by this class, including inherited public and protected methods. For more information on methods, see Methods in Class Design.
Events defined for the class, including all inherited events, returned as an array of matlab.metadata.Event
objects. Only handle classes can define events so this property is an empty matlab.metadata.Event
object for value classes. All handle
classes inherit theObjectBeingDestroyed
event. For more information about events, seeEvents.
Name and hidden status for enumeration members, returned as an array ofmatlab.metadata.EnumerationMember
objects. Access theName
and Hidden
properties of the corresponding member matlab.metadata.EnumerationMember
object to obtain information. For more information, see Enumerations.
Direct superclasses of this class, returned as an array ofmatlab.metadata.Class
objects. The matlab.metadata.Class
objects describe each direct superclass from which this class derives. For more information on subclassing, see Subclass Definition.
Methods
fromName | Return matlab.metadata.Class object that describes named class |
---|
These methods specialize standard MATLAB operators and functions for objects in this class.
eq (a == b) | Equality function (ClsA == ClsB). Use to test if two variables refer to equal classes (classes that contain exactly the same list of elements). |
---|---|
ne (a ~= b) | Not equal function (ClsA ~= ClsB). Use to test if two variables refer to different metaclasses. |
lt (a < b) | Less than function (ClsA < ClsB). Use to determine if ClsA is a strict subclass of ClsB. A strict subclass means ClsX < ClsX isfalse. |
le (a <= b) | Less than or equal to function (ClsA <= ClsB). Use to determine if ClsA is a subclass ofClsB. |
gt (a > b) | Greater than function (ClsA > ClsB). Use to determine if ClsA is a strict superclass ofClsB. A strict superclass means ClsX > ClsX is false). |
ge (a >= b) | Greater than or equal to function (ClsA >= ClsB). Use to determine if ClsA is a superclass ofClsB. |
Events
Event Name | Trigger | Event Data | Event Attributes |
---|---|---|---|
InstanceCreated | This event occurs every time an instance of the class described by thematlab.metadata.Class is created. The event occurs immediately after all constructor functions finish executing. | event.ClassInstanceEvent | NotifyAccess: privateListenAccess: public |
InstanceDestroyed | This event occurs every time an instance of the class described by thematlab.metadata.Class is destroyed.The event occurs immediately before any destructor functions execute. | event.ClassInstanceEvent | NotifyAccess: privateListenAccess: public |
Examples
Use introspection to get inheritance information about theIntrospectionExample
class.
classdef IntrospectionExample % IntrospectionExample Performs basic functions on two numbers % Class methods find the sum and product of its properties. properties % a First property % First of two numeric properties a {mustBeNumeric} = 0
% b Second property
% Second of two numeric properties
b {mustBeNumeric} = 0
end
methods
function sum = addNumbers(obj)
% addNumbers Sum the properties
% Finds the sum of properties a and b.
sum = obj.a + obj.b;
end
function prod = multNumbers(obj)
% multNumbers Multiply the properties
% Finds the product of properties a and b.
prod = obj.a*obj.b;
end
end
end
Create a metaclass instance for IntrospectionExample
.
mc = ?IntrospectionExample
mc =
Class with properties:
Name: 'IntrospectionExample'
Description: 'Performs basic functions on two numbers'
DetailedDescription: ' Class methods find the sum and product of its properties.'
Hidden: 0
Sealed: 0
Abstract: 0
Enumeration: 0
ConstructOnLoad: 0
HandleCompatible: 0
InferiorClasses: [0×1 matlab.metadata.Class]
Namespace: [0×0 matlab.metadata.Namespace]
Aliases: [0×1 string]
RestrictsSubclassing: 0
PropertyList: [2×1 matlab.metadata.Property]
MethodList: [4×1 matlab.metadata.Method]
EventList: [0×1 matlab.metadata.Event]
EnumerationMemberList: [0×1 matlab.metadata.EnumerationMember] SuperclassList: [0×1 matlab.metadata.Class]
The property summary for IntrospectionExample
shows there are four methods described in the MethodList
property. Access theMethodList
property of mc
and display the names of all four class methods.
for i = 1:4 mc.MethodList(i).Name end
ans = 'IntrospectionExample'
Version History
Introduced in R2008a
The namespace, class name, static method, and one property of meta.class
have been renamed. Their behavior remains the same.
- The new namespace is
matlab.metadata
. - The new class name is
Class
. - The static method
meta.class.fromName
is nowmatlab.metadata.Class.fromName
. - The
ContainingPackage
property is nowNamespace
.
MATLAB will continue to recognize the old metaclass names in most contexts. However, code that relies on string comparisons to identify metaclasses might need to be updated to continue to work as expected. For example, if mObj
in the example below is a matlab.metadata.Class
instance, the code under the case
statement for 'meta.class'
does not execute becauseclass(mObj)
returns 'matlab.metadata.Class'
.
switch class(mObj) case 'meta.class' % code to execute if mObj is a meta.class instance ... end
To ensure this code continues to work as intended, use an if
statement with isa
. The isa
command recognizes both the old and new names of the class.
if isa(mObj,'meta.class') % code to execute if mObj is a matlab.metadata.Class instance else ... end
If compatibility with older releases of MATLAB is not a concern, you can update 'meta.class'
to'matlab.metadata.Class'
. However, continue to use the old name if your code needs to run on versions of MATLAB before R2024a.
For user-defined classes with appropriately placed code comments, theDescription
and DetailedDescription
properties are populated with text pulled from those comments. For more information on how to use code comments to store custom help text for user-defined classes, see Custom Help Text.
The Aliases
property returns the aliases defined for the class.