Matlab Oop
Matlab Oop
Object-Oriented Programming
R2023b
How to Contact MathWorks
Phone: 508-647-7000
Basic Example
2
Creating a Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Design Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Create Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Access Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Call Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Add Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Vectorize Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Overload Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
BasicClass Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
v
Using BankAccount Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Static Data
4
Static Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
What Is Static Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Static Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Static Data Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
Constant Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
vi Contents
Special Considerations for Validation in Methods . . . . . . . . . . . . . . . . . . 5-10
vii
Defining and Organizing Classes
6
User-Defined Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
What Is a Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Attributes for Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Kinds of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
Constructing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
classdef Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
Class Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3
viii Contents
Creating and Managing Class Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27
Creating an Alias Definition File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27
Viewing Alias Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-28
Backward and Forward Compatibility of Aliases . . . . . . . . . . . . . . . . . . . 6-28
ix
Implement Copy for Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-30
Copy Method for Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-30
Customize Copy Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-31
Copy Properties That Contain Handles . . . . . . . . . . . . . . . . . . . . . . . . . . 7-32
Exclude Properties from Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33
x Contents
Property Get and Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-38
Property Get Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-38
Property Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-39
xi
Define Method in Function File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8
Specify Method Attributes in classdef File . . . . . . . . . . . . . . . . . . . . . . . . 9-9
Methods You Must Define in the classdef File . . . . . . . . . . . . . . . . . . . . . 9-10
Object Arrays
10
Create and Initialize Object Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Create an Object Array Using a Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Create an Object Array by Constructing the Last Element . . . . . . . . . . . . 10-2
Create an Object Array in the Constructor . . . . . . . . . . . . . . . . . . . . . . . 10-4
xii Contents
Accessing Properties and Methods in Object Arrays . . . . . . . . . . . . . . . . 10-6
Reference Properties in an Object Array . . . . . . . . . . . . . . . . . . . . . . . . . 10-6
Assigning Values to Properties in an Object Array . . . . . . . . . . . . . . . . . . 10-7
Invoking Methods on an Object Array . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8
xiii
Observe Changes to Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-8
xiv Contents
Using AbortSet with Property Validation . . . . . . . . . . . . . . . . . . . . . . . . 11-37
xv
Specify Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
Why Control Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
Specify Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
Define Sealed Hierarchy of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-22
xvi Contents
Subclasses of Built-In Types with Properties . . . . . . . . . . . . . . . . . . . . . 12-53
Specialized Numeric Types with Additional Data Storage . . . . . . . . . . . 12-53
Subclasses with Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-53
Property Added . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-53
Methods Implemented . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-53
Class Definition Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-54
Using ExtendDouble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-55
Concatenation of ExtendDouble Objects . . . . . . . . . . . . . . . . . . . . . . . . 12-58
xvii
Improve Version Compatibility with Default Values . . . . . . . . . . . . . . . . . 13-7
Version Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7
Using a Default Property Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7
Enumerations
14
Named Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Kinds of Predefined Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Techniques for Defining Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
xviii Contents
Refer to Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Instances of Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Conversion of Characters to Enumerations . . . . . . . . . . . . . . . . . . . . . . 14-10
Enumeration Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-12
xix
Constant Properties
15
Define Class Properties with Constant Values . . . . . . . . . . . . . . . . . . . . . . 15-2
Defining Named Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-2
Constant Property Assigned a Handle Object . . . . . . . . . . . . . . . . . . . . . 15-3
Constant Property Assigned Any Object . . . . . . . . . . . . . . . . . . . . . . . . . 15-4
Constant Properties — No Support for Get Events . . . . . . . . . . . . . . . . . 15-5
xx Contents
Object Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-5
Why Implement Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-5
Converters for Package Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-5
Converters and Subscripted Assignment . . . . . . . . . . . . . . . . . . . . . . . . . 17-6
Converter for Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-6
xxi
Customize Display for Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . 18-10
xxii Contents
DocPolynom Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2
DocPolynom Class Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-4
The DocPolynom Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-9
Convert DocPolynom Objects to Other Classes . . . . . . . . . . . . . . . . . . . 19-10
Overload disp for DocPolynom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-11
Display Evaluated Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-11
Define Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-12
Redefine Parentheses Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-13
xxiii
1
The MATLAB language enables you to create programs using both procedural and object-oriented
techniques and to use objects and ordinary functions together in your programs.
In procedural programming, your design focuses on the steps that must execute to achieve a desired
state. Typically, you represent data as individual variables or fields of a structure. You implement
operations as functions that take the variables as arguments. Programs usually call a sequence of
functions, each one of which is passed data, and then returns modified data. Each function performs
an operation or many operations on the data.
• Identifying the components of the system or application that you want to build
• Analyzing and identifying patterns to determine what components are used repeatedly or share
characteristics
• Classifying components based on similarities and differences
After performing this analysis, you define classes that describe the objects your application uses.
A class describes a set of objects with common characteristics. Objects are specific instances of a
class. The values contained in an object's properties are what make an object different from other
objects of the same class. The functions defined by the class (called methods) are what implement
object behaviors that are common to all objects of a class.
As functions become too large, you can break them into smaller functions and pass data from one to
function to another. However, as the number of functions becomes large, designing, and managing
the data passed to functions becomes difficult and error prone. At this point, consider moving your
MATLAB programming tasks to object-oriented designs.
1-2
Why Use Object-Oriented Design
Thinking in terms of objects is simpler and more natural for some problems. Think of the nouns in
your problem statement as the objects to define and the verbs as the operations to perform.
Consider the design of classes to represent money lending institutions (banks, mortgage companies,
individual money lenders, and so on). It is difficult to represent the various types of lenders as
procedures. However, you can represent each one as an object that performs certain actions and
contains certain data. The process of designing the objects involves identifying the characteristics of
a lender that are important to your application.
Identify Commonalities
What do all money lenders have in common? All MoneyLender objects can have a loan method and
an InterestRate property, for example.
Identify Differences
How does each money lender differ? One can provide loans to businesses while another provides
loans only to individuals. Therefore, the loan operation might need to be different for different types
of lending institutions. Subclasses of a base MoneyLender class can specialize the subclass versions
of the loan method. Each lender can have a different value for its InterestRate property.
Factor out commonalities into a superclass and implement what is specific to each type of lender in
the subclass.
Add Only What Is Necessary
These institutions might engage in activities that are not of interest to your application. During the
design phase, determine what operations and data an object must contain based on your problem
definition.
Objects provide several useful features not available from structures and cell arrays. For example,
objects can:
Reducing Redundancy
As the complexity of your program increases, the benefits of an object-oriented design become more
apparent. For example, suppose that you implement the following procedure as part of your
application:
1 Check inputs
2 Perform computation on the first input argument
3 Transform the result of step 2 based on the second input argument
4 Check validity of outputs and return values
You can implement this procedure as an ordinary function. But suppose that you use this procedure
again somewhere in your application, except that step 2 must perform a different computation. You
1-3
1 Using Object-Oriented Design in MATLAB
could copy and paste the first implementation, and then rewrite step 2. Or you could create a function
that accepted an option indicating which computation to make, and so on. However, these options
lead to more complicated code.
An object-oriented design can factor out the common code into what is called a base class. The base
class would define the algorithm used and implement whatever is common to all cases that use this
code. Step 2 could be defined syntactically, but not implemented, leaving the specialized
implementation to the classes that you then derive from this base class.
Step 1
function checkInputs()
% actual implementation
end
Step 2
function results = computeOnFirstArg()
% specify syntax only
end
Step 3
function transformResults()
% actual implementation
end
Step 4
function out = checkOutputs()
% actual implementation
end
The code in the base class is not copied or modified. Classes you derive from the base class inherit
this code. Inheritance reduces the amount of code to be tested, and isolates your program from
changes to the basic procedure.
The use of a class as the basis for similar, but more specialized classes is a useful technique in object-
oriented programming. This class defines a common interface. Incorporating this kind of class into
your program design enables you to:
Reducing Complexity
Objects reduce complexity by reducing what you must know to use a component or system:
To illustrate these advantages, consider the implementation of a data structure called a doubly linked
list. See “Implementing Linked Lists with Classes” on page 3-23 for the actual implementation.
1-4
Why Use Object-Oriented Design
To add a node to the list, disconnect the existing nodes in the list, insert the new node, and reconnect
the nodes appropriately. Here are the basic steps:
Now create the new node, connect it, and renumber the original nodes:
1 Link new.Prev to n1
2 Link new.Next to n3 (was n2)
3 Link n1.Next to new (will be n2)
4 Link n3.Prev to new (will be n2)
The details of how methods perform these steps are encapsulated in the class design. Each node
object contains the functionality to insert itself into or remove itself from the list.
For example, in this class, every node object has an insertAfter method. To add a node to a list,
create the node object and then call its insertAfter method:
nnew = NodeConstructor;
nnew.insertAfter(n1)
Because the node class defines the code that implements these operations, this code is:
The object methods enforce the rules for how the nodes interact. This design removes the
responsibility for enforcing rules from the applications that use the objects. It also means that the
application is less likely to generate errors in its own implementation of the process.
1-5
1 Using Object-Oriented Design in MATLAB
Fostering Modularity
As you decompose a system into objects (car –> engine –> fuel system –> oxygen sensor), you form
modules around natural boundaries. Classes provide three levels of control over code modularity:
• Public — Any code can access this particular property or call this method.
• Protected — Only this object's methods and methods of objects derived from this object's class can
access this property or call this method.
• Private — Only the object's own methods can access this property or call this method.
When you define a class, you can overload existing MATLAB functions to work with your new object.
For example, the MATLAB serial port class overloads the fread function to read data from the device
connected to the port represented by this object. You can define various operations, such as equality
(eq) or addition (plus), for a class you have defined to represent your data.
See Also
More About
• “Role of Classes in MATLAB” on page 3-2
1-6
Handle Object Behavior
More than one variable can refer to the same handle object. Therefore, users interact with instances
of handle classes differently than instances of value classes. Understanding how handle objects
behave can help you determine whether to implement a handle or a value class. This topic illustrates
some of those interactions.
What Is a Handle?
Certain kinds of MATLAB objects are handles. When a variable holds a handle, it actually holds a
reference to the object.
Handle objects enable more than one variable to refer to the same object. Handle-object behavior
affects what happens when you copy handle objects and when you pass them to functions.
Copies of Handles
All copies of a handle object variable refer to the same underlying object. This reference behavior
means that if h identifies a handle object, then,
h2 = h;
For example, the MATLAB audioplayer function creates a handle object that contains the audio
source data to reproduce a specific sound segment. The variable returned by the audioplayer
function identifies the audio data and enables you to access object functions to play the audio.
MATLAB software includes audio data that you can load and use to create an audioplayer object.
This sample load audio data, creates the audio player, and plays the audio:
load gong Fs y
gongSound = audioplayer(y,Fs);
play(gongSound)
Suppose that you copy the gongSound object handle to another variable (gongSound2):
gongSound2 = gongSound;
The variables gongSound and gongSound2 are copies of the same handle and, therefore, refer to the
same audio source. Access the audioplayer information using either variable.
1-7
1 Using Object-Oriented Design in MATLAB
For example, set the sample rate for the gong audio source by assigning a new value to the
SampleRate property. First get the current sample rate and then set a new sample rate:
sr = gongSound.SampleRate;
disp(sr)
8192
gongSound.SampleRate = sr*2;
disp(gongSound2.SampleRate)
16384
play(gongSound2)
Passing a nonhandle variable to a function does not affect the original variable that is in the caller’s
workspace. For example, myFunc modifies a local variable called var, but when the function ends,
the local variable var no longer exists:
function myFunc(var)
var = var + 1;
end
x = 12;
myFunc(x)
disp(x)
12
The myFunc function can return the modified value, which you could assign to the same variable
name (x) or another variable.
x = 12;
x = myFunc(x);
disp(x)
13
1-8
Handle Object Behavior
When the argument is a handle variable, the function copies only the handle, not the object identified
by that handle. Both handles (original and local copy) refer to the same object.
When the function modifies the data referred to by the object handle, those changes are accessible
from the handle variable in the calling workspace without the need to return the modified object.
For example, the modifySampleRate function changes the audioplayer sample rate:
function modifySampleRate(audioObj,sr)
audioObj.SampleRate = sr;
end
8192
modifySampleRate(gongSound,16384)
disp(gongSound.SampleRate)
16384
The modifySampleRate function does not need to return a modified gongSound object because
audioplayer objects are handle objects.
When a handle object has been deleted, the handle variables that referenced the object can still exist.
These variables become invalid because the object they referred to no longer exists. Calling delete
on the object removes the object, but does not clear handle variables.
The output argument, gongSound, is a handle variable. Calling delete deletes the object along with
the audio source information it contains:
1-9
1 Using Object-Oriented Design in MATLAB
delete(gongSound)
disp(gongSound)
whos
Fs 1x1 8 double
gongSound 1x1 0 audioplayer
y 42028x1 336224 double
Note The value for Bytes returned by the whos command does not include the data referenced by a
handle because many variables can reference the same data.
The handle gongSound no longer refers to a valid object, as shown by the isvalid handle method:
isvalid(gongSound)
ans =
logical
Calling delete on a deleted handle does nothing and does not cause an error. You can pass an array
containing both valid and invalid handles to delete. MATLAB deletes the valid handles, but does not
issue an error when encountering handles that are already invalid.
gongSound.SampleRate
play(gongSound)
clear gongSound
whos
Fs 1x1 8 double
y 42028x1 336224 double
1-10
Handle Object Behavior
See Also
More About
• “Handle Class Destructor” on page 7-13
• “Comparison of Handle and Value Classes” on page 7-2
1-11
2
Basic Example
2 Basic Example
In this section...
“Design Class” on page 2-2
“Create Object” on page 2-3
“Access Properties” on page 2-3
“Call Methods” on page 2-3
“Add Constructor” on page 2-4
“Vectorize Methods” on page 2-4
“Overload Functions” on page 2-5
“BasicClass Code Listing” on page 2-6
Design Class
The basic purpose of a class is to define an object that encapsulates data and the operations
performed on that data. For example, BasicClass defines a property and two methods that operate
on the data in that property:
• Value — Property that contains the numeric data stored in an object of the class
• roundOff — Method that rounds the value of the property to two decimal places
• multiplyBy — Method that multiplies the value of the property by the specified number
Start a class definition with a classdef ClassName...end block, and then define the class
properties and methods inside that block. Here is the definition of BasicClass:
classdef BasicClass
properties
Value {mustBeNumeric}
end
methods
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value]*n;
end
end
end
• Save the class definition in a .m file with the same name as the class.
• Create an object of the class.
• Access the properties to assign data.
• Call methods to perform operation on the data.
2-2
Creating a Simple Class
Create Object
Create an object of the class using the class name:
a = BasicClass
a =
Value: []
Access Properties
Assign a value to the Value property using the object variable and a dot before the property name:
a.Value = pi/3;
a.Value
ans =
1.0472
Call Methods
Call the roundOff method on object a:
roundOff(a)
ans =
1.0500
Pass the object as the first argument to a method that takes multiple arguments, as in this call to the
multiplyBy method:
multiplyBy(a,3)
ans =
3.1416
a.multiplyBy(3)
Passing the object as an explicit argument is not necessary when using dot notation. The notation
uses the object to the left of the dot.
2-3
2 Basic Example
Add Constructor
Classes can define a special method to create objects of the class, called a constructor. Constructor
methods enable you to pass arguments to the constructor, which you can assign as property values.
The BasicClass Value property restricts its possible values using the mustBeNumeric function.
Here is a constructor for the BasicClass class. When you call the constructor with an input
argument, it is assigned to the Value property. If you call the constructor without an input argument,
the Value property has a default value of empty ([]).
methods
function obj = BasicClass(val)
if nargin == 1
obj.Value = val;
end
end
end
By adding this constructor to the class definition, you can create an object and set the property value
in one step:
a = BasicClass(pi/3)
a =
Value: 1.0472
The constructor can perform other operations related to creating objects of the class.
Vectorize Methods
MATLAB enables you to vectorize operations. For example, you can add a number to a vector:
[1 2 3] + 2
ans =
3 4 5
MATLAB adds the number 2 to each of the elements in the array [1 2 3]. To vectorize the
arithmetic operator methods, enclose the obj.Value property reference in brackets.
[obj.Value] + 2
This syntax enables the method to work with arrays of objects. For example, create an object array
using indexed assignment.
obj(1) = BasicClass(2.7984);
obj(2) = BasicClass(sin(pi/3));
obj(3) = BasicClass(7);
2-4
Creating a Simple Class
[obj.Value] + 2
[obj(1).Value obj(2).Value obj(3).Value] + 2
The roundOff method is vectorized because the property reference is enclosed in brackets.
r = round([obj.Value],2);
roundOff(obj)
ans =
Overload Functions
Classes can implement existing functionality, such as addition, by defining a method with the same
name as the existing MATLAB function. For example, suppose that you want to add two BasicClass
objects. It makes sense to add the values of the Value properties of each object.
Here is an overloaded version of the MATLAB plus function. It defines addition for the BasicClass
class as adding the property values:
methods
function r = plus(o1,o2)
r = [o1.Value] + [o2.Value];
end
end
By implementing a method called plus, you can use the “+” operator with objects of BasicClass.
a = BasicClass(pi/3);
b = BasicClass(pi/4);
a + b
ans =
1.8326
a = BasicClass(pi/3);
b = BasicClass(pi/4);
c = BasicClass(pi/2);
ar = [a b];
ar + c
ans =
2.6180 2.3562
Related Information
For information on overloading functions, see “Overload Functions in Class Definitions” on page 9-
25.
2-5
2 Basic Example
classdef BasicClass
properties
Value {mustBeNumeric}
end
methods
function obj = BasicClass(val)
if nargin == 1
obj.Value = val;
end
end
function r = roundOff(obj)
r = round([obj.Value],2);
end
function r = multiplyBy(obj,n)
r = [obj.Value] * n;
end
function r = plus(o1,o2)
r = [o1.Value] + [o2.Value];
end
end
end
See Also
Related Examples
• “Components of a Class” on page 5-2
• “Validate Property Values” on page 8-18
2-6
3
Classes
In the MATLAB language, every value is assigned to a class. For example, creating a variable with an
assignment statement constructs a variable of the appropriate class:
a = 7;
b = 'some text';
s.Name = 'Nancy';
s.Age = 64;
whos
whos
Name Size Bytes Class Attributes
a 1x1 8 double
b 1x9 18 char
s 1x1 370 struct
Basic commands like whos display the class of each value in the workspace. This information helps
MATLAB users recognize that some values are characters and display as text while other values are
double precision numbers, and so on. Some variables can contain different classes of values like
structures.
Predefined Classes
MATLAB defines fundamental classes that comprise the basic types used by the language. These
classes include numeric, logical, char, cell, struct, and function handle.
User-Defined Classes
You can create your own MATLAB classes. For example, you could define a class to represent
polynomials. This class could define the operations typically associated with MATLAB classes, like
addition, subtraction, indexing, displaying in the command window, and so on. These operations
would need to perform the equivalent of polynomial addition, polynomial subtraction, and so on. For
example, when you add two polynomial objects:
p1 + p2
the plus operation must be able to add polynomial objects because the polynomial class defines this
operation.
When you define a class, you can overload special MATLAB functions (such as plus.m for the
addition operator). MATLAB calls these methods when users apply those operations to objects of your
class.
See “Representing Polynomials with Classes” on page 19-2 for an example that creates just such a
class.
3-2
Role of Classes in MATLAB
MATLAB classes use the following words to describe different parts of a class definition and related
concepts.
Classes
A class is a definition that specifies certain characteristics that all instances of the class share. These
characteristics are determined by the properties, methods, and events that define the class and the
values of attributes that modify the behavior of each of these class components. Class definitions
describe how objects of the class are created and destroyed, what data the objects contain, and how
you can manipulate this data.
Class Hierarchies
It sometimes makes sense to define a new class in terms of existing classes. This approach enables
you to reuse the designs and techniques in a new class that represents a similar entity. You
accomplish this reuse by creating a subclass. A subclass defines objects that are a subset of those
objects defined by the superclass. A subclass is more specific than its superclass and might add new
properties, methods, and events to those components inherited from the superclass.
Mathematical sets can help illustrate the relationships among classes. In the following diagram, the
set of Positive Integers is a subset of the set of Integers and a subset of Positives. All three sets are
subsets of Reals, which is a subset of All Numbers.
The definition of Positive Integers requires the additional specification that members of the set be
greater than zero. Positive Integers combine the definitions from both Integers and Positives. The
resulting subset is more specific, and therefore more narrowly defined, than the supersets, but still
shares all the characteristics that define the supersets.
3-3
3 MATLAB Classes Overview
The “is a” relationship is a good way to determine if it is appropriate to define a particular subset in
terms of existing supersets. For example, each of the following statements makes senses:
If the “is a” relationship holds, then it is likely you can define a new class from a class or classes that
represent some more general case.
Reusing Solutions
Classes are usually organized into taxonomies to foster code reuse. For example, if you define a class
to implement an interface to the serial port of a computer, it would probably be similar to a class
designed to implement an interface to the parallel port. To reuse code, you could define a superclass
that contains everything that is common to the two types of ports, and then derive subclasses from
the superclass in which you implement only what is unique to each specific port. Then the subclasses
would inherit all the common functionality from the superclass.
Objects
A class is like a template for the creation of a specific instance of the class. This instance or object
contains actual data for a particular entity that is represented by the class. For example, an instance
of a bank account class is an object that represents a specific bank account, with an actual account
number and an actual balance. This object has built into it the ability to perform operations defined
by the class, such as making deposits to and withdrawals from the account balance.
Objects are not just passive data containers. Objects actively manage the data contained by allowing
only certain operations to be performed, by hiding data that does not need to be public, and by
preventing external clients from misusing data by performing operations for which the object was not
designed. Objects even control what happens when they are destroyed.
3-4
Role of Classes in MATLAB
Encapsulating Information
An important aspect of objects is that you can write software that accesses the information stored in
the object via its properties and methods without knowing anything about how that information is
stored, or even whether it is stored or calculated when queried. The object isolates code that
accesses the object from the internal implementation of methods and properties. You can define
classes that hide both data and operations from any methods that are not part of the class. You can
then implement whatever interface is most appropriate for the intended use.
References
[1] Shalloway, A., J. R. Trott, Design Patterns Explained A New Perspective on Object-Oriented
Design.. Boston, MA: Addison-Wesley 2002.
[2] Gamma, E., R. Helm, R. Johnson, J. Vlissides, Design Patterns Elements of Reusable Object-
Oriented Software. Boston, MA: Addison-Wesley 1995.
[3] Freeman, E., Elisabeth Freeman, Kathy Sierra, Bert Bates, Head First Design Patterns.
Sebastopol, CA 2004.
See Also
Related Examples
• “Creating a Simple Class” on page 2-2
• “Developing Classes That Work Together” on page 3-6
• “Representing Structured Data with Classes” on page 3-14
• “Implementing Linked Lists with Classes” on page 3-23
3-5
3 MATLAB Classes Overview
Formulating a Class
This example discusses how to approach the design and implementation of two classes that interact
through events and listeners. The two classes represent a bank account and an account manager.
To design a class that represents a bank account, first determine the elements of data and the
operations that form your abstraction of a bank account. For example, a bank account has:
• An account number
• An account balance
• A status (open, closed, etc.)
If the balance is too low and you attempt to withdraw money, the bank account broadcasts a notice.
When this event occurs, the bank account broadcasts a notice to other entities that are designed to
listen for these notices. In this example, a simplified version of an account manager program
performs this task.
In this example, an account manager program determines the status of all bank accounts. This
program monitors the account balance and assigns one of three values:
These features define the requirements of the BankAccount and AccountManager classes. Include
only what functionality is required to meet your specific objectives. Support special types of accounts
by subclassing BankAccount and adding more specific features to the subclasses. Extend the
AccountManager as required to support new account types.
3-6
Developing Classes That Work Together
Class Data
The class defines these properties to store the account number, account balance, and the account
status:
• AccountNumber — A property to store the number identifying the specific account. MATLAB
assigns a value to this property when you create an instance of the class. Only BankAccount class
methods can set this property. The SetAccess attribute is private.
• AccountBalance — A property to store the current balance of the account. The class operation
of depositing and withdrawing money assigns values to this property. Only BankAccount class
methods can set this property. The SetAccess attribute is private.
• AccountStatus — The BankAccount class defines a default value for this property. The
AccountManager class methods change this value whenever the value of the AccountBalance
falls below 0. The Access attribute specifies that only the AccountManager and BankAccount
classes have access to this property.
• AccountListener — Storage for the InsufficentFunds event listener. Saving a BankAccount
object does not save this property because you must recreate the listener when loading the object.
Class Operations
• BankAccount — Accepts an account number and an initial balance to create an object that
represents an account.
• deposit — Updates the AccountBalance property when a deposit transaction occurs
• withdraw — Updates the AccountBalance property when a withdrawal transaction occurs
• getStatement — Displays information about the account
• loadobj — Recreates the account manager listener when you load the object from a MAT-file.
Class Events
The account manager program changes the status of bank accounts that have negative balances. To
implement this action, the BankAccount class triggers an event when a withdrawal results in a
negative balance. Therefore, the triggering of the InsufficientsFunds event occurs from within
the withdraw method.
To define an event, specify a name within an events block. Trigger the event by a call to the notify
handle class method. Because InsufficientsFunds is not a predefined event, you can name it with
any char vector and trigger it with any action.
3-7
3 MATLAB Classes Overview
example, different values for the account balance. Therefore, implement the BankAccount class as a
handle class. All copies of a given handle object refer to the same data.
3-8
Developing Classes That Work Together
End of classdef
3-9
3 MATLAB Classes Overview
The AccountManager class stores no data so it does not need properties. The BankAccount object
stores the handle of the listener object.
Class Components
• assignStatus — Method that assigns a status to a BankAccount object. Serves as the listener
callback.
• addAccount — Method that creates the InsufficientFunds listener.
3-10
Developing Classes That Work Together
• Extend the AccountManager class to support other types of accounts while keeping the
individual account classes simple and specialized.
• Change the criteria for the account status without affecting the compatibility of saved and loaded
BankAccount objects.
• Develop an Account superclass that factors out what is common to all accounts without requiring
each subclass to implement the account management functionality
3-11
3 MATLAB Classes Overview
BA.AccountStatus = 'overdrawn';
end
end
end
function lh = addAccount(BA)
lh = addlistener(BA, 'InsufficientFunds', ...
@(src, ~)AccountManager.assignStatus(src));
end
end
end
BA = BankAccount(1234567,500)
BA =
AccountNumber: 1234567
AccountBalance: 500
AccountListener: [1x1 event.listener]
getStatement(BA)
-------------------------
Account: 1234567
CurrentBalance: 500.00
Account Status: open
-------------------------
withdraw(BA,600)
getStatement(BA)
-------------------------
Account: 1234567
CurrentBalance: -100.00
Account Status: overdrawn
-------------------------
The $600 withdrawal triggered the InsufficientsFunds event. The current criteria defined by the
AccountManager class results in a status of overdrawn.
withdraw(BA,200)
getStatement(BA)
-------------------------
Account: 1234567
CurrentBalance: -300.00
3-12
Developing Classes That Work Together
Now the AccountStatus has been set to closed by the listener and further attempts to make
withdrawals are blocked without triggering the event:
withdraw(BA,100)
deposit(BA,700)
getStatement(BA)
-------------------------
Account: 1234567
CurrentBalance: 400.00
Account Status: open
-------------------------
3-13
3 MATLAB Classes Overview
For this example, the data represents tensile stress/strain measurements. These data are used to
calculate the elastic modulus of various materials. In simple terms, stress is the force applied to a
material and strain is the resulting deformation. Their ratio defines a characteristic of the material.
While this approach is an over simplification of the process, it suffices for this example.
Data Description
Material char vector identifying the type of material tested
SampleNumber Number of a particular test sample
Stress Vector of numbers representing the stress applied to the
sample during the test.
Strain Vector of numbers representing the strain at the
corresponding values of the applied stress.
Modulus Number defining an elastic modulus of the material under test,
which is calculated from the stress and strain data
3-14
Representing Structured Data with Classes
This example begins with a simple implementation of the class and builds on this implementation to
illustrate how features enhance the usefulness of the class.
The first version of the class provides only data storage. The class defines a property for each of the
required data elements.
classdef TensileData
properties
Material
SampleNumber
Stress
Strain
Modulus
end
end
Treat the TensileData object (td in the previous statements) much as you would any MATLAB
structure. However, defining a specialized data structure as a class has advantages over using a
general-purpose data structure, like a MATLAB struct:
• Users cannot accidentally misspell a field name without getting an error. For example, typing the
following:
td.Modulus = ...
would simply add a field to a structure. However, it returns an error when td is an instance of the
TensileData class.
• A class is easy to reuse. Once you have defined the class, you can easily extend it with subclasses
that add new properties.
• A class is easy to identify. A class has a name so that you can identify objects with the whos and
class functions and the Workspace browser. The class name makes it easy to refer to records
with a meaningful name.
• A class can validate individual field values when assigned, including class or value.
• A class can restrict access to fields, for example, allowing a particular field to be read, but not
changed.
Restrict properties to specific values by defining a property set access method. MATLAB calls the set
access method whenever setting a value for a property.
3-15
3 MATLAB Classes Overview
The Material property set method restricts the assignment of the property to one of the following
strings: aluminum, stainless steel, or carbon steel.
classdef TensileData
properties
Material
SampleNumber
Stress
Strain
Modulus
end
methods
function obj = set.Material(obj,material)
if (strcmpi(material,'aluminum') ||...
strcmpi(material,'stainless steel') ||...
strcmpi(material,'carbon steel'))
obj.Material = material;
else
error('Invalid Material')
end
end
end
end
When there is an attempt to set the Material property, MATLAB calls the set.Material method
before setting the property value.
If the value matches the acceptable values, the function set the property to that value. The code
within set method can access the property directly to avoid calling the property set method
recursively.
For example:
td = TensileData;
td.Material = 'brass';
methods
function td = TensileData(material,samplenum,stress,strain)
if nargin > 0
3-16
Representing Structured Data with Classes
td.Material = material;
td.SampleNumber = samplenum;
td.Stress = stress;
td.Strain = strain;
end
end
end
Create a TensileData object fully populated with data using the following statement:
td = TensileData('carbon steel',1,...
[2e4 4e4 6e4 8e4],...
[.12 .20 .31 .40]);
If the value of a property depends on the values of other properties, define that property using the
Dependent attribute. MATLAB does not store the values of dependent properties. The dependent
property get method determines the property value when the property is accessed. Access can occur
when displaying object properties or as the result of an explicit query.
Calculating Modulus
TensileData objects do not store the value of the Modulus property. The constructor does not have
an input argument for the value of the Modulus property. The value of the Modulus:
Therefore, it is better to calculate the value of the Modulus property only when its value is
requested. Use a property get access method to calculate the value of the Modulus.
The Modulus property depends on Stress and Strain, so its Dependent attribute is true. Place
the Modulus property in a separate properties block and set the Dependent attribute.
The get.Modulus method calculates and returns the value of the Modulus property.
properties (Dependent)
Modulus
end
Define the property get method in a methods block using only default attributes.
methods
function modulus = get.Modulus(obj)
ind = find(obj.Strain > 0);
modulus = mean(obj.Stress(ind)./obj.Strain(ind));
end
end
This method calculates the average ratio of stress to strain data after eliminating zeros in the
denominator data.
3-17
3 MATLAB Classes Overview
MATLAB calls the get.Modulus method when the property is queried. For example,
td = TensileData('carbon steel',1,...
[2e4 4e4 6e4 8e4],...
[.12 .20 .31 .40]);
td.Modulus
ans =
1.9005e+005
To set the value of a Dependent property, the class must implement a property set method. There is
no need to allow explicit setting of the Modulus property. However, a set method enables you to
provide a customized error message. The Modulus set method references the current property value
and then returns an error:
methods
function obj = set.Modulus(obj,~)
fprintf('%s%d\n','Modulus is: ',obj.Modulus)
error('You cannot set the Modulus property');
end
end
The TensileData class overloads the disp method. This method controls object display in the
command window.
The disp method displays the value of the Material, SampleNumber, and Modulus properties. It
does not display the Stress and Strain property data. These properties contain raw data that is not
easily viewed in the command window.
The disp method uses fprintf to display formatted text in the command window:
methods
function disp(td)
fprintf(1,...
'Material: %s\nSample Number: %g\nModulus: %1.5g\n',...
td.Material,td.SampleNumber,td.Modulus);
end
end
It is useful to view a graph of the stress/strain data to determine the behavior of the material over a
range of applied tension. The TensileData class overloads the MATLAB plot function.
The plot method creates a linear graph of the stress versus strain data and adds a title and axis
labels to produce a standardized graph for the tensile data records:
methods
function plot(td,varargin)
plot(td.Strain,td.Stress,varargin{:})
title(['Stress/Strain plot for Sample',...
3-18
Representing Structured Data with Classes
num2str(td.SampleNumber)])
ylabel('Stress (psi)')
xlabel('Strain %')
end
end
The first argument to this method is a TensileData object, which contains the data.
The method passes a variable list of arguments (varargin) directly to the built-in plot function. The
TensileData plot method allows you to pass line specifier arguments or property name-value
pairs.
For example:
td = TensileData('carbon steel',1,...
[2e4 4e4 6e4 8e4],[.12 .20 .31 .40]);
plot(td,'-+b','LineWidth',2)
3-19
3 MATLAB Classes Overview
3-20
Representing Structured Data with Classes
classdef TensileData
properties
Material
SampleNumber
Stress
Strain
end
properties (Dependent)
Modulus
end
methods
function td = TensileData(material,samplenum,stress,strain)
if nargin > 0
td.Material = material;
td.SampleNumber = samplenum;
td.Stress = stress;
td.Strain = strain;
end
end
function m = get.Modulus(obj)
ind = find(obj.Strain > 0);
m = mean(obj.Stress(ind)./obj.Strain(ind));
end
3-21
3 MATLAB Classes Overview
function disp(td)
sprintf('Material: %s\nSample Number: %g\nModulus: %1.5g\n',...
td.Material,td.SampleNumber,td.Modulus)
end
function plot(td,varargin)
plot(td.Strain,td.Stress,varargin{:})
title(['Stress/Strain plot for Sample ',...
num2str(td.SampleNumber)])
xlabel('Strain %')
ylabel('Stress (psi)')
end
end
end
See Also
More About
• “Components of a Class” on page 5-2
3-22
Implementing Linked Lists with Classes
To use the class, create a folder named @dlnode and save dlnode.m to this folder. The parent folder
of @dlnode must be on the MATLAB path. Alternatively, save dlnode.m to a path folder.
• Data array
• Handle to the next node
• Handle to the previous node
Class Properties
The dlnode class implements each node as a handle object with three properties:
This diagram shows a list with three-nodes n1, n2, and n3. It also shows how the nodes reference the
next and previous nodes.
3-23
3 MATLAB Classes Overview
Class Methods
• dlnode — Construct a node and assign the value passed as an input to the Data property
• insertAfter — Insert this node after the specified node
• insertBefore — Insert this node before the specified node
• removeNode — Remove this node from the list and reconnect the remaining nodes
• clearList — Remove large lists efficiently
• delete — Private method called by MATLAB when deleting the list.
Build these nodes into a doubly linked list using the class methods designed for this purpose:
n2.insertAfter(n1) % Insert n2 after n1
n3.insertAfter(n2) % Insert n3 after n2
ans =
Data: 2
Next: [1x1 dlnode]
Prev: [1x1 dlnode]
ans =
Data: 2
Next: [1x1 dlnode]
Prev: [1x1 dlnode]
n1.Next.Next % Points to n3
ans =
Data: 3
Next: []
Prev: [1x1 dlnode]
3-24
Implementing Linked Lists with Classes
n3.Prev.Prev % Points to n1
ans =
Data: 1
Next: [1x1 dlnode]
Prev: []
For example, a node object, node, contains in its Next property the handle of the next node object,
node.Next. Similarly, the Prev property contains the handle of the previous node, node.Prev.
Using the three-node linked list defined in the previous section, you can demonstrate that the
following statements are true:
n1.Next == n2
n2.Prev == n1
x = n2;
x == n1.Next
x.Prev == n1
But each instance of a node is unique so there is only one node in the list that can satisfy the
conditions of being equal to n1.Next and having a Prev property that contains a handle to n1.
Therefore, x must point to the same node as n2.
There has to be a way for multiple variables to refer to the same object. The MATLAB handle class
provides a means for both x and n2 to refer to the same node.
The handle class defines the eq method (use methods('handle') to list the handle class methods),
which enables the use of the == operator with all handle objects.
Related Information
For more information on handle classes, see “Comparison of Handle and Value Classes” on page 7-
2.
3-25
3 MATLAB Classes Overview
3-26
Implementing Linked Lists with Classes
methods
function node = dlnode(Data)
% Construct a dlnode object
if nargin > 0
node.Data = Data;
end
end
3-27
3 MATLAB Classes Overview
if ~isempty(nodeAfter.Prev)
nodeAfter.Prev.Next = newNode;
end
nodeAfter.Prev = newNode;
end
function removeNode(node)
% Remove a node from a linked list.
if ~isscalar(node)
error('Input must be scalar')
end
prevNode = node.Prev;
nextNode = node.Next;
if ~isempty(prevNode)
prevNode.Next = nextNode;
end
if ~isempty(nextNode)
nextNode.Prev = prevNode;
end
node.Next = dlnode.empty;
node.Prev = dlnode.empty;
end
function clearList(node)
% Clear the list before
% clearing list variable
prev = node.Prev;
next = node.Next;
removeNode(node)
while ~isempty(next)
node = next;
next = node.Next;
removeNode(node);
end
while ~isempty(prev)
node = prev;
prev = node.Prev;
removeNode(node)
end
end
end
Class Properties
Only dlnode class methods can set the Next and Prev properties because these properties have
private set access (SetAccess = private). Using private set access prevents client code from
performing any incorrect operation with these properties. The dlnode class methods perform all the
operations that are allowed on these nodes.
The Data property has public set and get access, allowing you to query and modify the value of Data
as required.
3-28
Implementing Linked Lists with Classes
properties
Data
end
properties(SetAccess = private)
Next = dlnode.empty;
Prev = dlnode.empty;
end
To create a node object, specify the node's data as an argument to the constructor:
Insert Nodes
There are two methods for inserting nodes into the list — insertAfter and insertBefore. These
methods perform similar operations, so this section describes only insertAfter in detail.
First, insertAfter calls the removeNode method to ensure that the new node is not connected to
any other nodes. Then, insertAfter assigns the newNode Next and Prev properties to the handles
of the nodes that are after and before the newNode location in the list.
For example, suppose that you want to insert a new node, nnew, after an existing node, n1, in a list
containing n1—n2—n3.
nnew = dlnode(rand(3));
Next, call insertAfter to insert nnew into the list after n1:
nnew.insertAfter(n1)
The insertAfter method performs the following steps to insert nnew in the list between n1 and n2:
nnew.Next = n1.Next;
3-29
3 MATLAB Classes Overview
• Set nnew.Prev to n1
nnew.Prev = n1;
• If n1.Next is not empty, then n1.Next is still n2, so n1.Next.Prev is n2.Prev, which is set to
nnew
n1.Next.Prev = nnew;
• n1.Next is now set to nnew
n1.Next = nnew;
Remove a Node
The removeNode method removes a node from a list and reconnects the remaining nodes. The
insertBefore and insertAfter methods always call removeNode on the node to insert before
attempting to connect it to a linked list.
Calling removeNode ensures that the node is in a known state before assigning it to the Next or
Prev property:
function removeNode(node)
if ~isscalar(node)
error('Input must be scalar')
end
prevNode = node.Prev;
nextNode = node.Next;
if ~isempty(prevNode)
prevNode.Next = nextNode;
end
if ~isempty(nextNode)
nextNode.Prev = prevNode;
end
node.Next = dlnode.empty;
node.Prev = dlnode.empty;
end
For example, suppose that you remove n2 from a three-node list (n1–n2–n3):
n2.removeNode;
3-30
Implementing Linked Lists with Classes
removeNode removes n2 from the list and reconnects the remaining nodes with the following steps:
n1 = n2.Prev;
n3 = n2.Next;
if n1 exists, then
n1.Next = n3;
if n3 exists, then
n3.Prev = n1
The list is rejoined because n1 connects to n3 and n3 connects to n1. The final step is to ensure that
n2.Next and n2.Prev are both empty (that is, n2 is not connected):
n2.Next = dlnode.empty;
n2.Prev = dlnode.empty;
Suppose that you create a list with 10 nodes and save the handle to the head of the list:
head = dlnode(1);
for i = 10:-1:2
new = dlnode(i);
insertAfter(new,head);
end
Now remove the third node (Data property assigned the value 3):
removeNode(head.Next.Next)
head.Next.Next
ans =
Data: 4
Next: [1x1 dlnode]
Prev: [1x1 dlnode]
head.Next
3-31
3 MATLAB Classes Overview
ans =
Data: 2
Next: [1x1 dlnode]
Prev: [1x1 dlnode]
Delete a Node
To delete a node, call the removeNode method on that node. The removeNode method disconnects
the node and reconnects the list before allowing MATLAB to destroy the removed node. MATLAB
destroys the node once references to it by other nodes are removed and the list is reconnected.
When you create a linked list and assign a variable that contains, for example, the head or tail of the
list, clearing that variable causes the destructor to recurse through the entire list. With large enough
list, clearing the list variable can result in MATLAB exceeding its recursion limit.
The clearList method avoids recursion and improves the performance of deleting large lists by
looping over the list and disconnecting each node. clearList accepts the handle of any node in the
list and removes the remaining nodes.
function clearList(node)
if ~isscalar(node)
error('Input must be scalar')
end
prev = node.Prev;
next = node.Next;
removeNode(node)
while ~isempty(next)
node = next;
next = node.Next;
3-32
Implementing Linked Lists with Classes
removeNode(node);
end
while ~isempty(prev)
node = prev;
prev = node.Prev;
removeNode(node)
end
end
For example, suppose that you create a list with many nodes:
head = dlnode(1);
for k = 100000:-1:2
nextNode = dlnode(k);
insertAfter(nextNode,head)
end
The variable head contains the handle to the node at the head of the list:
head
head =
Data: 1
Next: [1x1 dlnode]
Prev: []
head.Next
ans =
Data: 2
Next: [1x1 dlnode]
Prev: [1x1 dlnode]
The only nodes that have not been deleted by MATLAB are those nodes for which there exists an
explicit reference. In this case, those references are head and nextNode:
head
head =
Data: 1
Next: []
Prev: []
nextNode
nextNode =
3-33
3 MATLAB Classes Overview
Data: 2
Next: []
Prev: []
The delete method has private access to prevent users from calling delete when intending to
delete a single node. MATLAB calls delete implicitly when the list is destroyed.
To delete a single node from the list, use the removeNode method.
The dlnode class implements a doubly linked list and provides a convenient starting point for
creating more specialized types of linked lists. For example, suppose that you want to create a list in
which each node has a name.
Rather than copying the code used to implement the dlnode class, and then expanding upon it, you
can derive a new class from dlnode (that is, subclass dlnode). You can create a class that has all the
features of dlnode and also defines its own additional features. And because dlnode is a handle
class, this new class is a handle class too.
To use the class, create a folder named @NamedNode and save NamedNode.m to this folder. The
parent folder of @NamedNode must be on the MATLAB path. Alternatively, save NamedNode.m to a
path folder.
The following class definition shows how to derive the NamedNode class from the dlnode class:
3-34
Implementing Linked Lists with Classes
n.Name = name;
end
end
end
The NamedNode class adds a Name property to store the node name.
The constructor calls the class constructor for the dlnode class, and then assigns a value to the Name
property.
Use the NamedNode class like the dlnode class, except that you specify a name for each node object.
For example:
Now use the insert methods inherited from dlnode to build the list:
n(2).insertAfter(n(1))
n(3).insertAfter(n(2))
A single node displays its name and data when you query its properties:
n(1).Next
ans =
n(1).Next.Next
ans =
n(3).Prev.Prev
ans =
3-35
3 MATLAB Classes Overview
See Also
More About
• “The Handle Superclass” on page 7-11
3-36
Working with Objects in MATLAB
Some MATLAB® functions return objects. Objects combine data (properties) with functions and
methods. Object properties contain data, including simple types like numbers or text, or other
objects. The functions and methods perform actions on the objects themselves. These functions can
act on the object properties or change the state of the object, for example.
Create an Object
When creating an object, you can assign a variable to that object. The variable provides access to the
properties and methods of the object. For example, this syntax for the histogram function not only
displays a histogram of the data in x but also returns the object as the output h.
h = histogram(x)
Create a histogram object that displays 1000 random numbers. Calling histogram with an output
argument displays the graph, the type or class of the object (Histogram), and a partial list of the
object properties and their values.
x = randn(1000,1);
h = histogram(x)
h =
Histogram with properties:
3-37
3 MATLAB Classes Overview
In the workspace, the histogram object is listed with the other active variables, including the
dimensions and the type of object.
Object properties contain data. By changing property values, you can modify certain aspects of an
object. You can use dot notation with the variable assigned to the object to access and change its
properties.
In the case of a histogram object, properties contain the raw data, the number of bins, the height of
each bar, and other information that controls the appearance of the histogram. For example,
Orientation is a property of histogram objects that determines whether the bars are displayed
horizontally or vertically. You can access the value of the property by entering the name of the object
(h), a dot, and the property name.
h.Orientation
ans =
'vertical'
You can change the value of the property using the same syntax, but with the addition of an equal
sign and the new value.
h.Orientation = "horizontal"
3-38
Working with Objects in MATLAB
h =
Histogram with properties:
Not all object properties are writeable. A property can be read-only, meaning that it can be read, but
trying to assign a new value to it returns an error. For example, the Values property of a histogram
stores the height of each bar and is calculated when the object is created. It is a read-only property,
so you cannot change Values directly.
Some functions are designed to perform actions on an object. These functions can be methods, which
are defined specifically for one class of objects, or can be functions that accept that object as an
ordinary input argument. In either case, you can use the variable for the object as an input. For
example, Histogram objects include the functions morebins and fewerbins, which increase and
3-39
3 MATLAB Classes Overview
decrease the number of histogram bins, respectively. Access the NumBins property to see how many
bins are currently in the histogram.
h.NumBins
ans = 23
Call morebins to increase the number of bins in histogram h. The morebins function increases the
number of bins by approximately 10%, so the number of bins increases from 23 to 26.
morebins(h)
ans = 26
Other object-oriented programming languages frequently use dot notation to call methods, such as
h.morebins. That syntax also is supported for methods in MATLAB. However, for consistency in
sample code, the documentation uses function form for most methods and functions that accept
object inputs.
The morebins function does not have any additional input arguments, but that is not true of all
functions that accept objects. Besides the object itself, object functions can have additional input
arguments, which you can pass to the function with standard function syntax. For example, you can
call to an object function that takes two input arguments with this syntax:
functionName(objectVariable,arg1,arg2)
3-40
Working with Objects in MATLAB
In addition to the objects provided in the MATLAB language, you can define your own class-based
objects using object-oriented programming techniques. The language follows standard OO
conventions. For more information, start with “Creating a Simple Class” on page 2-2.
See Also
“Fundamental MATLAB Classes” | “Creating a Simple Class” on page 2-2
3-41
4
Static Data
4 Static Data
Static Data
In this section...
“What Is Static Data” on page 4-2
“Static Variable” on page 4-2
“Static Data Object” on page 4-3
“Constant Data” on page 4-4
Static Variable
Classes can use a persistent variable to store static data. Define a static method or local function in
which you create a persistent variable. The method or function provides access to this variable. Use
this technique when you want to store one or two variables.
Saving an object of the class defining the persistent variable does not save the static data associated
with the class. To save your static data in an object, or define more extensive data, use the static data
object technique “Static Data Object” on page 4-3
Implementation
The StoreData class defines a static method that declares a persistent variable Var. The
setgetVar method provides set and get access to the data in the persistent variable. Because the
setgetVar method has public access, you can set and get the data stored in the persistent variable
globally. Control the scope of access by setting the method Access attribute.
classdef StoreData
methods (Static)
function out = setgetVar(data)
persistent Var;
if nargin
Var = data;
end
out = Var;
end
end
end
Set the value of the variable by calling setgetVar with an input argument. The method assigns the
input value to the persistent variable:
StoreData.setgetVar(10);
Get the value of the variable by calling setgetVar with no input argument:
a = StoreData.setgetVar
4-2
Static Data
a =
10
a =
[]
Add a method like setgetVar to any class in which you want the behavior of a static property.
Implementation
The SharedData class is a handle class, which enables you to reference the same object data from
multiple handle variables:
classdef SharedData < handle
properties
Data1
Data2
end
end
The UseData class is the stub of a class that uses the data stored in the SharedData class. The
UseData class stores the handle to a SharedData object in a constant property.
classdef UseData
properties (Constant)
Data = SharedData
end
% Class code here
end
The Data property contains the handle of the SharedData object. MATLAB constructs the
SharedData object when loading the UseData class. All subsequently created instances of the
UseData class refer to the same SharedData object.
To initialize the SharedData object properties, load the UseData class by referencing the constant
property.
h = UseData.Data
h =
4-3
4 Static Data
Data1: []
Data2: []
Use the handle to the SharedData object to assign data to property values:
h.Data1 = 'MyData1';
h.Data2 = 'MyData2';
Each instance of the UseData class refers to the same handle object:
a1 = UseData;
a2 = UseData;
a1.Data.Data1
ans =
MyData1
a1.Data.Data1 = rand(3);
All new and existing objects of the UseData class share the same SharedData object. a2 now has
the rand(3) data that was assigned to a1 in the previous step:
a2.Data.Data1
ans =
To reinitialize the constant property, clear all instances of the UseData class and then clear the class:
clear a1 a2
clear UseData
Constant Data
To store constant values that do not change, assign the data to a constant property. All instances of
the class share the same value for that property. Control the scope of access to constant properties by
setting the property Access attribute.
The only way to change the value of a constant property is to change the class definition. Use
constant properties like public final static fields in Java®.
See Also
persistent | clear
4-4
Static Data
Related Examples
• “Define Class Properties with Constant Values” on page 15-2
• “Static Methods” on page 9-23
More About
• “Method Attributes” on page 9-4
• “Property Attributes” on page 8-8
• “Static Properties” on page 5-38
4-5
5
Components of a Class
In this section...
“Class Building Blocks” on page 5-2
“Class Definition Block” on page 5-2
“Properties Block” on page 5-3
“Methods Block” on page 5-3
“Events Block” on page 5-4
“Attribute Specification” on page 5-4
“Enumeration Classes” on page 5-5
“Related Information” on page 5-6
properties, methods, events, and enumeration are keywords only within a classdef block.
For example, this classdef defines a class called MyClass that subclasses the handle class. The
class is also defined as sealed, so you cannot use inherit from this class.
5-2
Components of a Class
Properties Block
A properties block contains property definitions, including optional initial values. Use a separate
block for each unique set of attribute specifications. Each properties block starts with the
properties keyword and terminates with the end keyword.
properties (PropertyAttributes)
PropertyName size class {validators} = DefaultValue
end
For example, this class defines a private property Prop1 of type double with a default value.
classdef MyClass
properties (SetAccess = private)
Prop1 double = 12
end
...
end
Methods Block
A methods block contains function definitions for the class methods. Use a separate block for each
unique set of attribute specifications. Each methods block starts with the methods keyword and
terminates with the end keyword.
methods (MethodAttributes)
function obj = MethodName(arg1,...)
...
end
classdef MyClass
methods (Access = protected)
function obj = myMethod(obj,arg1)
...
end
end
end
MATLAB differs from languages like C++ and Java in that you must explicitly pass an object of the
class to the method.
Using the MyClass example, call MyMethod using the object obj of the class and either function or
dot syntax:
obj = MyClass;
r = MyMethod(obj,arg1);
r = obj.MyMethod(arg1);
5-3
5 Class Definition—Syntax Reference
Events Block
The events block (one for each unique set of attribute specifications) contains the names of events
that this class declares. The events block starts with the events keyword and terminates with the
end keyword.
classdef ClassName
events (EventAttributes)
EventName
end
...
end
For example, this class defined an event called StateChange with ListenAccess set to
protected.
classdef EventSource
events (ListenAccess = protected)
StateChanged
end
...
end
Attribute Specification
Attribute Syntax
Attributes modify the behavior of classes and class components (properties, methods, and events).
Attributes enable you to define useful behaviors without writing complicated code. For example, you
can create a read-only property by setting its SetAccess attribute to private but leaving its
GetAccess attribute set to public.
All class definition blocks (classdef, properties, methods, and events) support specific
attributes. All attributes have default values. Specify attribute values only in cases where you want to
change from the default value.
Note Specify the value of a particular attribute only once in any component block.
Attribute Descriptions
5-4
Components of a Class
Attribute Values
When you specify attribute values, those values affect all the components defined within the defining
block. Defining properties with different attribute settings requires multiple properties blocks.
Specify multiple attributes in a comma-separated list.
properties (SetObservable = true)
AccountBalance
end
You can use a simpler syntax for attributes whose values are true or false. The attribute name
alone implies true and adding the not operator (~) to the name implies false. For example, these two
ways of defining a static methods block are equivalent.
methods (Static)
...
end
Similarly, these three ways of defining a nonstatic methods block are equivalent. All attributes that
take a logical value have a default value of false, so you can omit the attribute to get the default
behavior.
methods
...
end
methods (~Static)
...
end
Enumeration Classes
Enumeration classes are specialized classes that define a fixed set of names representing a single
type of value. Enumeration classes use an enumeration block that contains the enumeration
members defined by the class.
The enumeration block starts with the enumeration keyword and terminates with the end keyword.
classdef ClassName < SuperClass
enumeration
EnumerationMember
end
5-5
5 Class Definition—Syntax Reference
...
end
For example, this class defines two enumeration members that represent the logical values false
and true.
Related Information
“Creating a Simple Class” on page 2-2
5-6
Method Syntax
Method Syntax
In this section...
“Methods Definition Block” on page 5-7
“Method Argument Validation” on page 5-8
“Special Considerations for Validation in Methods” on page 5-10
This topic describes how to define class methods in MATLAB using methods...end blocks, and it
introduces method argument validation. This topic focuses on nonstatic, concrete methods, also
referred to as ordinary methods. For other types of methods, see:
methods (attributes)
function method1(obj,arg1,...)
...
end
function method2(obj,arg1,...)
...
end
...
end
With the exception of static methods, you must pass an object of the class explicitly to a MATLAB
method.
For example, this class defines one public property and two public methods. Each method takes two
input arguments: the object itself and a user-provided argument inputArg. The methods calculate
the product and quotient of the value of the class property, Property1, and the input argument.
classdef methodDemo
properties
Property1
end
methods
function prod = propMultiply(obj,inputArg)
prod = obj.Property1*inputArg;
end
function quotient = propDivide(obj,inputArg)
quotient = obj.Property1/inputArg;
end
5-7
5 Class Definition—Syntax Reference
end
end
You can also define multiple method blocks with different attributes. In this example, the first method
is protected, and the second method is private. For more information, see “Method Attributes” on
page 9-4.
classdef attributeDemo
methods (Access = protected)
function out = method1(obj,inputArg)
...
end
end
methods (Access = private)
function out = method2(obj,inputArg)
...
end
end
end
Input argument validation enables you to restrict the size, class, and other characteristics of a
method input argument. The syntax for input argument validation is:
arguments
argName1 (dimensions) class {validators} = defaultValue
...
end
• (dimensions) — Input size, specified as a comma-separated list of two or more numbers, such
as (1,2).
• class — Class, specified as a class name, such as double or the name of a user-defined class.
• {validators} — Comma-separated list of validation functions, such as mustBeNumeric and
mustBeScalarOrEmpty, enclosed in curly brackets. For a list of validation functions, see
“Argument Validation Functions”.
• defaultValue — Default values must conform to the specified size, type, and validation rules.
See arguments for the full description of elements in input argument validation syntax.
Input argument validation is useful for methods with public access. Restricting the types of argument
values allowed from callers of the method can prevent errors before the body of the method is
executed. For example, the Rectangle class represents a rectangle in the coordinate plane, with
properties specifying its location (X and Y) as well as its width and height.
classdef Rectangle
properties
X (1,1) double {mustBeReal} = 0
Y (1,1) double {mustBeReal} = 0
5-8
Method Syntax
methods
function R = enlarge(R,x,y)
arguments (Input)
R (1,1) Rectangle
x (1,1) {mustBeNonnegative}
y (1,1) {mustBeNonnegative}
end
arguments (Output)
R (1,1) Rectangle
end
R.Width = R.Width + x;
R.Height = R.Height + y;
end
end
end
The enlarge method increases the height and width of the rectangle by adding the user inputs x and
y to Width and Height, respectively. Because the intent of the method is to enlarge one rectangle,
the validation restricts the input arguments to scalar values with a (1,1) size restriction and
nonnegative numeric values with mustBeNonnegative.
Instantiate the class, and call enlarge with inputs 5 and -1 to enlarge. The argument validation
returns an error.
rect1 = Rectangle;
rect1.enlarge(5,-1))
Tip The (Input) attribute for an input arguments block is optional, but it is recommended for
readability when defining both input and output argument blocks in a single method. MATLAB
interprets an argument block with neither attribute as an input argument block.
The syntax for output argument validation is the same as input validation, except that you must
specify (Output) as an attribute of the arguments block, and you cannot set a default value.
arguments (Output)
argName1 (dimensions) class {validators}
...
end
See arguments for the full description of the output argument validation syntax.
Output argument validation enables class authors to document what type of outputs a method
returns, as well as acting as a fail-safe against future changes to the code that might alter the output
types. For example, the enlarge method of Rectangle uses output argument validation to ensure
that the object method returns a scalar instance of Rectangle.
5-9
5 Class Definition—Syntax Reference
arguments (Output)
R (1,1) Rectangle
end
If enlarge were later revised to return only the dimensions of the Rectangle and not the object
itself, for example, the output validation would help catch this potential mistake.
• If a classdef file includes method prototypes for methods defined in separate files, any
arguments blocks you want to define for those methods must be defined in the separate files. For
more information on defining methods in separate files, see “Methods in Separate Files” on page
9-8.
• Subclass methods do not inherit argument validation. To preserve argument validation in subclass
methods that override superclass methods, repeat the argument validation from the superclass
method in the subclass method.
• Abstract methods do not support argument validation because they cannot define arguments
blocks. For more information, see “Abstract Classes and Class Members” on page 12-68.
• Handle class destructor methods cannot use argument validation. In handle classes, a destructor
method named delete is called by MATLAB when a handle object becomes unreachable or is
explicitly deleted with a call to delete. A destructor must have exactly one input, no outputs, and
no argument validation. MATLAB treats a method defined in any other way as an ordinary method
and does not use it as a destructor. For more information, see “Handle Class Destructor” on page
7-13.
See Also
arguments
Related Examples
• “Function Argument Validation”
5-10
Call Superclass Methods on Subclass Objects
superMethod@MySuperClass(obj,superMethodArguments)
For example, a subclass can call a superclass disp method to implement the display of the superclass
part of the object. Then the subclass adds code to display the subclass part of the object:
obj = obj@MySuperClass(SuperClassArguments)
In this class, the MySub object is initialized by the MySuperClass constructor. The superclass
constructor constructs the MySuperClass part of the object using the specified arguments.
5-11
5 Class Definition—Syntax Reference
obj = obj@MySuperClass(SuperClassArguments);
...
end
end
end
See Also
Related Examples
• “Modify Inherited Methods” on page 12-13
5-12
Using a Class to Display Graphics
To use the CircleArea class, copy this code into a file named CircleArea.m and save this file in a
folder that is on the MATLAB path.
classdef CircleArea
properties
Radius
end
properties (Constant)
P = pi
end
properties (Dependent)
Area
end
methods
function obj = CircleArea(r)
if nargin > 0
obj.Radius = r;
end
end
function val = get.Area(obj)
val = obj.P*obj.Radius^2;
end
function obj = set.Radius(obj,val)
if val < 0
error('Radius must be positive')
end
obj.Radius = val;
end
function plot(obj)
r = obj.Radius;
d = r*2;
pos = [0 0 d d];
curv = [1 1];
rectangle('Position',pos,'Curvature',curv,...
'FaceColor',[.9 .9 .9])
line([0,r],[r,r])
text(r/2,r+.5,['r = ',num2str(r)])
title(['Area = ',num2str(obj.Area)])
axis equal
end
function disp(obj)
rad = obj.Radius;
disp(['Circle with radius: ',num2str(rad)])
5-13
5 Class Definition—Syntax Reference
end
end
methods (Static)
function obj = createObj
prompt = {'Enter the Radius'};
dlgTitle = 'Radius';
rad = inputdlg(prompt,dlgTitle);
r = str2double(rad{:});
obj = CircleArea(r);
end
end
end
ca = CircleArea.createObj
ca.Area
ans =
164.2202
plot(ca)
5-14
Using a Class to Display Graphics
5-15
5 Class Definition—Syntax Reference
methods
function obj = CircleArea(r)
if nargin > 0
obj.Radius = r;
else
obj.Radius = 0;
end
end
Because the Area property is Dependent, the class does not store its value. The get.Area method
calculates the value of the Area property whenever it is queried. (“Get and Set Methods for
Dependent Properties” on page 8-42)
function val = get.Area(obj)
val = obj.P*obj.Radius^2;
end
The set.Radius method tests the value assigned to the Radius property to ensure that the value is
not less than zero. MATLAB calls set.Radius to assign a value to Radius. (“Property Get and Set
Methods” on page 8-38)
function obj = set.Radius(obj,val)
if val < 0
error('Radius must be positive')
end
obj.Radius = val;
end
The CircleArea class overloads the plot function. The plot method uses the rectangle function
to create a circle and draws the radius. (“Overload Functions in Class Definitions” on page 9-25)
function plot(obj)
r = obj.Radius;
d = r*2;
pos = [0 0 d d];
curv = [1 1];
rectangle('Position',pos,'Curvature',curv)
line([0,r],[r,r])
text(r/2,r+.5,['r = ',num2str(r)])
axis equal
end
The CircleArea class overloads the disp function to change the way MATLAB displays objects in
the command window.
function disp(obj)
rad = obj.Radius;
disp(['Circle with radius: ',num2str(rad)])
end
end
methods (Static)
The CircleArea class defines a Static method that uses a dialog box to create an object. (“Static
Methods” on page 9-23)
function obj = createObj
prompt = {'Enter the Radius'};
dlgTitle = 'Radius';
rad = inputdlg(prompt,dlgTitle);
r = str2double(rad{:});
obj = CircleArea(r);
end
5-16
Using a Class to Display Graphics
5-17
5 Class Definition—Syntax Reference
While the previous function is legal MATLAB code, it results in Code Analyzer warnings for two
reasons:
The Code Analyzer returns only one warning, suggesting that you might actually want to refer to the
EmployeeName property.
While this version of someMethod is legal MATLAB code, it is confusing to give a property the same
name as a function. Therefore, the Code Analyzer provides a warning suggesting that you might have
intended the statement to be:
5-18
MATLAB Code Analyzer Warnings
EN = obj.EmployeeName;
The Code Analyzer does not warn when a variable name is the same as a property name when the
variable is:
In these particular cases, the Code Analyzer does not warn you that you are using a variable name
that is also a property name. Therefore, a coding error like the following:
See Also
Related Examples
• “Use of Editor and Debugger with Classes” on page 5-25
5-19
5 Class Definition—Syntax Reference
The values returned by the eq method must be of type logical or convertible to logical. MATLAB
attempts to convert the output of eq to a logical value if the output of the eq method is a nonlogical
value.
Note You do not need to define eq methods for enumeration classes. See “Enumerations in Switch
Statements” on page 5-23.
All classes derived from the handle class inherit an eq method. The expression,
h1 == h2
5-20
Objects in Conditional Statements
end
end
The switch statements work only with scalar objects. For example:
h1(1) = BasicHandle('Handle Object');
h1(2) = BasicHandle('Handle Object');
h1(3) = BasicHandle('Handle Object');
h2 = h1;
switch h1
case h2
disp('h2 is selected')
otherwise
disp('h2 not selected')
end
In this case, h1 is not scalar. Use isscalar to determine if an object is scalar before entering a
switch statement.
Some MATLAB functions also use the built-in == operator in their implementation. Therefore, your
implementation of eq should be replaceable with the built-in eq to enable objects of your class work
like built-in types in MATLAB code.
Design of eq
Implement the eq method to return a logical array representing the result of the == comparison.
5-21
5 Class Definition—Syntax Reference
For example, the SwitchOnVer class implements an eq method that returns true for the ==
operation if the value of the Version property is the same for both objects. In addition, eq works
with arrays the same way as the built-in eq. For the following expression:
obj1 == obj2
Implementation of eq
Here is a class that implements an eq method. Ensure that your implementation contains appropriate
error checking for the intended use.
classdef SwitchOnVer
properties
Version
end
methods
function obj = SwitchOnVer(ver)
if nargin > 0
obj.Version = ver;
end
end
function bol = eq(obj1,obj2)
if ~strcmp(class(obj1),class(obj2))
error('Objects are not of the same class')
end
s1 = numel(obj1);
s2 = numel(obj2);
if s1 == s2
bol = false(size(obj1));
for k=1:s1
if obj1(k).Version == obj2(k).Version
bol(k) = true;
else
bol(k) = false;
end
end
elseif s1 == 1
bol = scalarExpEq(obj2,obj1);
elseif s2 == 1
bol = scalarExpEq(obj1,obj2);
else
error('Dimension missmatch')
end
function ret = scalarExpEq(ns,s)
% ns is nonscalar array
% s is scalar array
ret = false(size(ns));
n = numel(ns);
for kk=1:n
5-22
Objects in Conditional Statements
if ns(kk).Version == s.Version
ret(kk) = true;
else
ret(kk) = false;
end
end
end
end
end
end
...
if isscalar(objIn)
switch(objIn)
case ov1
disp('This is version 1.0')
case ov2
disp('This is version 2.0')
case ov3
disp('This is version 3.0')
otherwise
disp('There is no version')
end
else
error('Input object must be scalar')
end
For example, the WeeklyPlanner class defines enumerations for five days of the week. The switch/
case statements in the todaySchedule static method dispatch on the enumeration member
corresponding to the current day of the week. The datetime, day, and string functions return a
string containing the name of the current day.
classdef WeeklyPlanner
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
methods (Static)
function todaySchedule
dayName = string(day(datetime("name"));
dayEnum = WeeklyPlanner.(dayName);
switch dayEnum
case WeeklyPlanner.Monday
disp("Monday schedule")
case WeeklyPlanner.Tuesday
5-23
5 Class Definition—Syntax Reference
disp("Tuesday schedule")
case WeeklyPlanner.Wednesday
disp("Wednesday schedule")
case WeeklyPlanner.Thursday
disp("Thursday schedule")
case WeeklyPlanner.Friday
disp("Friday schedule")
end
end
end
end
WeeklyPlanner.todaySchedule
Enumeration classes that derived from built-in types inherit the superclass eq method. For example,
the FlowRate class derives from int32:
The switchEnum function switches on the input argument, which can be a FlowRate enumeration
value.
function switchEnum(inpt)
switch inpt
case 10
disp('Flow = 10 cfm')
case 50
disp('Flow = 50 cfm')
case 100
disp('Flow = 100 cfm')
end
end
switchEnum(FlowRate.Medium)
Flow = 50 cfm
5-24
Use of Editor and Debugger with Classes
+PackFld1/+PackFld2/@myclass/myclass.m
To open myclass.m in the MATLAB editor, you could reference the file using dot-separated package
names:
edit PackFld1.PackFld2.myclass
edit +PackFld1/+PackFld2/@myclass/myclass
edit +PackFld1/+PackFld2/myclass
edit PackFld1.PackFld2.packFunction
edit +PackFld1/+PackFld2/packFunction
To refer to a method defined in its own file inside a class folder, use:
edit +PackFld1/+PackFld2/@myclass/myMethod
dbstop in myclass at 14
5-25
5 Class Definition—Syntax Reference
See “Automatic Updates for Modified Classes” on page 5-27 for information about clearing class
after modification.
See Also
dbstop
Related Examples
• “MATLAB Code Analyzer Warnings” on page 5-18
• “Debug MATLAB Code Files”
5-26
Automatic Updates for Modified Classes
In this section...
“When MATLAB Loads Class Definitions” on page 5-27
“Consequences of Automatic Update” on page 5-27
“What Happens When Class Definitions Change” on page 5-28
“Ensure Defining Folder Remains in Scope” on page 5-28
“Actions That Do Not Trigger Updates” on page 5-29
“Multiple Updates to Class Definitions” on page 5-29
“Object Validity with Deleted Class File” on page 5-29
“When Updates Are Not Possible” on page 5-29
“Potential Consequences of Class Updates” on page 5-29
“Interactions with the Debugger” on page 5-30
“Updates to Class Attributes” on page 5-30
“Updates to Property Definitions” on page 5-31
“Updates to Method Definitions” on page 5-31
“Updates to Event Definitions” on page 5-32
• The first time the class is referenced, such as creating an instance, accessing a constant property,
or calling a static method of the class.
• Whenever the definition of a loaded class changes and MATLAB returns to the command prompt.
• When you change the MATLAB path and cause a different definition of the class to be used. The
change takes effect after MATLAB returns to the command prompt.
• Whenever you access the class metadata.
MATLAB allows only one definition for a class to exist at any time. Therefore, MATLAB attempts to
update all existing objects of a class automatically to conform to the new class definition. You do not
need to call clear classes to remove existing objects when you change their defining class.
Note Using an editor other than the MATLAB editor or using MATLAB Online™ can result in delays
to automatic updating.
5-27
5 Class Definition—Syntax Reference
Here is an example of what happens when you create an instance of a concrete class edit the class
definition to make the class abstract.
a = MyClass;
% Edit MyClass to make it Abstract
Note MATLAB does not update metaclass instances when you change the definition of a class. You
must get new metaclass data after updating a class definition.
• Value change to handle — Existing objects become independent handles referring to different
objects.
• Enumeration member added — Existing objects preserve the enumeration members they had
previously, even if the underlying values have changed.
• Enumeration member removed — Existing objects that are not using the removed member have
the same enumeration members that they had previously. Existing objects that use the removed
member replace the removed member with the default member of the enumeration.
• Enumeration block removed — Enumeration members are taken out of use.
• Superclass definition changed — Changes applied to all subclasses in the hierarchy of that
superclass.
• Superclass added or removed — Change of superclass applied to all existing objects.
For example, suppose the class of the input obj is defined in the current folder, which is not on the
path. Before changing the current folder to another folder, add the current folder to the path using
the addpath function.
function runFromTempFolder(obj)
% Add current folder to path
addpath(pwd)
definingFolder = cd('myTempFolder');
obj.myMethod;
cd(definingFolder)
end
5-28
Automatic Updates for Modified Classes
Objects do not update until referenced in a way that exposes the change, such as invoking the object
display or assigning to a property.
Some class updates cause situations in which MATLAB cannot update existing objects to conform to a
modified class definition. These cases result in errors until you delete the objects:
5-29
5 Class Definition—Syntax Reference
• Removing a class from a heterogeneous class hierarchy can result in invalid heterogeneous array
elements. In this case, the default object for the heterogeneous hierarchy replaces these array
elements.
MATLAB disables the debugger during class updates. Before R2021a, a breakpoint could potentially
interrupt the class update process and allow for the introduction of errors when the update resumes.
For example, the breakpoint allows a class author to introduce invalid syntax into the class definition
or remove the class from the path entirely, potentially causing MATLAB to crash.
classdef ClassWithBreakpoint
properties (Constant)
Prop1 (1,1) {myPropertyValidator} = 32
end
end
function myPropertyValidator(~)
end % Add breakpoint here
Create an instance of this class. Then add a breakpoint where indicated, and update the definition of
Prop1 to a different initial value:
In version R2020b and earlier, MATLAB hits the breakpoint, and the class update is interrupted. In
R2021a, the debugger is disabled, and the breakpoint does not interrupt the update.
Change Effect
Make Abstract = true Accessing existing objects returns an error.
Change AllowedSubclasses Newly created objects can inherit from different superclasses
than existing objects.
Change ConstructOnLoad Loading classes obeys the current value of ConstructOnLoad.
Change HandleCompatible Newly created objects can have different class hierarchy than
existing objects.
Change Hidden Appearance of class in list of superclasses and access by help
function can change
Change InferiorClasses Method dispatching for existing objects can change.
Make Sealed = true Existing subclass objects return errors when accessed.
5-30
Automatic Updates for Modified Classes
Change Effect
Add property Adds the new property to existing objects of the class. Sets the
property values to the default value (which is [] if the class
definition does not specify a default).
Remove property Removes the property from existing objects of the class.
Attempts to access the removed property fail.
Change property default value Does not apply the new default value to existing objects of the
class.
Move property between subclass Does not apply different default value when property definition
and superclass moves between superclass and subclass.
Change property attribute value Applies changes to existing objects of the class.
Change Effect
Add method You can call the new method on existing objects of the class.
Modify method Modifications are available to existing objects.
Remove method You can on longer call deleted method on existing objects.
5-31
5 Class Definition—Syntax Reference
Change Effect
Change method attribute value Apply changes to existing objects of the class.
5-32
Automatic Updates for Modified Classes
Change Effect
Change event attribute value Apply changes to existing objects of the class.
See Also
Related Examples
• “Use of Editor and Debugger with Classes” on page 5-25
5-33
5 Class Definition—Syntax Reference
Public Properties
Unlike fields in C++ or the Java language, you can use MATLAB properties to define a public
interface separate from the implementation of data storage. You can provide public access to
properties because you can define set and get access methods that execute automatically when
assigning or querying property values. For example, the following statement:
myobj.Material = 'plastic';
assigns the char vector plastic to the Material property of myobj. Before making the actual
assignment, myobj executes a method called set.Material (assuming the class of myobj defines
this method), which can perform any necessary operations. See “Property Get and Set Methods” on
page 8-38 for more information on property access methods.
You can also control access to properties by setting attributes, which enable public, protected, or
private access. See “Property Attributes” on page 8-8 for a full list of property attributes.
No Implicit Parameters
In some languages, one object parameter to a method is always implicit. In MATLAB, objects are
explicit parameters to the methods that act on them.
Dispatching
In MATLAB classes, method dispatching is not based on method signature, as it is in C++ and Java
code. When the argument list contains objects of equal precedence, MATLAB uses the leftmost object
to select the method to call.
However, if the class of an argument is superior to the class of the other arguments, MATLAB
dispatches to the method of the superior argument, regardless of its position within the argument
list.
• In C++, you call a superclass method using the scoping operator: superclass::method
• In Java code, you use: superclass.method
5-34
Comparison of MATLAB and Other OO Languages
Other Differences
In MATLAB classes, there is no equivalent to C++ templates or Java generics. However, MATLAB is
weakly typed and it is possible to write functions and classes that work with different types of data.
MATLAB classes do not support overloading functions using different signatures for the same
function name.
Object Modification
MATLAB classes can define public properties, which you can modify by explicitly assigning values to
those properties on a given instance of the class. However, only classes derived from the handle
class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes
not derived from handle), changes the value only within the context in which the modification is
made.
MATLAB passes all variables by value. When you pass an object to a function, MATLAB copies the
value from the caller into the parameter variable in the called function.
However, MATLAB supports two kinds of classes that behave differently when copied:
• Handle classes — a handle class instance variable refers to an object. A copy of a handle class
instance variable refers to the same object as the original variable. If a function modifies a handle
object passed as an input argument, the modification affects the object referenced by both the
original and copied handles.
• Value classes — the property data in an instance of a value class are independent of the property
data in copies of that instance (although, a value class property could contain a handle). A
function can modify a value object that is passed as an input argument, but this modification does
not affect the original object.
See “Comparison of Handle and Value Classes” on page 7-2 for more information on the behavior
and use of both kinds of classes.
Passing Value Objects
When you pass a value object to a function, the function creates a local copy of the argument
variable. The function can modify only the copy. If you want to modify the original object, return the
modified object and assign it to the original variable name. For example, consider the value class,
SimpleClass :
classdef SimpleClass
properties
Color
end
methods
function obj = SimpleClass(c)
if nargin > 0
obj.Color = c;
end
5-35
5 Class Definition—Syntax Reference
end
end
end
Pass the object to the function g, which assigns blue to the Color property:
function y = g(x)
x.Color = 'blue';
y = x;
end
y = g(obj);
The function g modifies its copy of the input object and returns that copy, but does not change the
original object.
y.Color
ans =
blue
obj.Color
ans =
red
If the function g did not return a value, the modification of the object Color property would have
occurred only on the copy of obj within the function workspace. This copy would have gone out of
scope when the function execution ended.
When you pass a handle to a function, the function makes a copy of the handle variable, just like
when passing a value object. However, because a copy of a handle object refers to the same object as
the original handle, the function can modify the object without having to return the modified object.
For example, suppose that you modify the SimpleClass class definition to make a class derived from
the handle class:
classdef SimpleHandleClass < handle
properties
Color
end
methods
function obj = SimpleHandleClass(c)
if nargin > 0
obj.Color = c;
end
end
5-36
Comparison of MATLAB and Other OO Languages
end
end
Pass the object to the function g, which assigns blue to the Color property:
y = g(obj);
The function g sets the Color property of the object referred to by both the returned handle and the
original handle:
y.Color
ans =
blue
obj.Color
ans =
blue
ans =
yellow
The function g modified the object referred to by the input argument (obj) and returned a handle to
that object in y.
MATLAB Passes Handles by Value
Handles do not behave like references in C++. If you pass an object handle to a function and that
function assigns a different object to that handle variable, the variable in the caller is not affected.
For example, suppose you define a function g2:
function y = g2(x)
x = SimpleHandleClass('green');
y = x;
end
ans =
green
5-37
5 Class Definition—Syntax Reference
obj.Color
ans =
red
The function overwrites the handle passed in as an argument, but does not overwrite the object
referred to by the handle. The original handle obj still references the original object.
Static Properties
In MATLAB, classes can define constant properties, but not "static" properties in the sense of other
languages like C++. You cannot change constant properties from the initial value specified in the
class definition.
MATLAB has long-standing rules that variables always take precedence over the names of functions
and classes. Assignment statements introduce a variable if one does not exist.
A.B = C
Introduce a new variable, A, that is a struct containing a field B whose value is C. If A.B = C could
refer to a static property of class A, then class A would take precedence over variable A.
This behavior would be a significant incompatibility with prior releases of MATLAB. For example, the
introduction of a class named A on the MATLAB path could change the meaning of an assignment
statement like A.B = C inside a .m code file.
In other languages, classes rarely use static data, except as private data within the class or as public
constants. In MATLAB, you can use constant properties the same way you use public final static
fields in Java. To use data that is internal to a class in MATLAB, create persistent variables in private
or protected methods or local functions used privately by the class.
Avoid static data in MATLAB. If a class has static data, using the same class in multiple applications
causes conflicts among applications. Conflicts are less of an issue in some other languages. These
languages compile applications into executables that run in different processes. Each process has its
own copy of the class static data. MATLAB, frequently runs many different applications in the same
process and environment with a single copy of each class.
For ways to define and use static data in MATLAB, see “Static Data” on page 4-2.
5-38
Comparison of MATLAB and Other OO Languages
5-39
6
User-Defined Classes
In this section...
“What Is a Class Definition” on page 6-2
“Attributes for Class Members” on page 6-2
“Kinds of Classes” on page 6-2
“Constructing Objects” on page 6-3
“Class Hierarchies” on page 6-3
“classdef Syntax” on page 6-3
“Class Code” on page 6-3
Define MATLAB classes in code blocks, with subblocks delineating the definitions of various class
members. For syntax information on these blocks, see “Components of a Class” on page 5-2.
Class definitions can provide information, such as inheritance relationships or the names of class
members without actually constructing the class. See “Class Metadata” on page 16-2.
Kinds of Classes
There are two kinds of MATLAB classes—handle classes and value classes.
• Value classes represent independent values. Value objects contain the object data and do not
share this data with copies of the object. MATLAB numeric types are value classes. Values objects
passed to and modified by functions must return a modified object to the caller.
• Handle classes create objects that reference the object data. Copies of the instance variable refer
to the same object. Handle objects passed to and modified by functions affect the object in the
caller’s workspace without returning the object.
For more information, see “Comparison of Handle and Value Classes” on page 7-2.
6-2
User-Defined Classes
Constructing Objects
For information on class constructors, see “Class Constructor Methods” on page 9-15.
For information on creating arrays of objects, see “Create and Initialize Object Arrays” on page 10-
2.
Class Hierarchies
For more information on how to define class hierarchies, see “Hierarchies of Classes — Concepts” on
page 12-2.
classdef Syntax
Class definitions are blocks of code that are delineated by the classdef keyword at the beginning
and the end keyword at the end. Files can contain only one class definition.
The following diagram shows the syntax of a classdef block. Only comments and blank lines can
precede the classdef keyword.
Class Code
Here is a simple class definition with one property and a constructor method that sets the value of the
property when there is an input argument supplied.
classdef MyClass
properties
Prop
end
methods
function obj = MyClass(val)
if nargin > 0
obj.Prop = val;
end
6-3
6 Defining and Organizing Classes
end
end
end
To create an object of MyClass, save the class definition in a .m file having the same name as the
class and call the constructor with any necessary arguments:
d = datestr(now);
o = MyClass(d);
o.Prop
ans =
10-Nov-2005 10:38:14
The constructor should support a no argument syntax so MATLAB can create default objects. For
more information, see “No Input Argument Constructor Requirement” on page 9-18.
For more information on the components of a class definition, see “Components of a Class” on page 5-
2
See Also
Related Examples
• “Creating a Simple Class” on page 2-2
• “Developing Classes That Work Together” on page 3-6
• “Representing Structured Data with Classes” on page 3-14
6-4
Class Attributes
Class Attributes
In this section...
“Specifying Class Attributes” on page 6-5
“Specifying Attributes” on page 6-7
“Class-Specific Attributes” on page 6-7
6-5
6 Defining and Organizing Classes
Class Attributes
(default =
false)
Framework attributes Classes that use certain framework base classes have framework-specific attributes.
See the documentation for the specific base class you are using for information on
these attributes.
6-6
Class Attributes
Specifying Attributes
Attributes are specified for class members in the classdef, properties, methods, and events
definition blocks. The particular attribute setting applies to all members defined within that
particular block. You can use multiple properties, methods, and events definition blocks to apply
different attribute setting to different class members.
Class attributes settings are not inherited, so superclass attribute values do not affect subclasses.
Attribute Syntax
Specify class attribute values in parentheses, separating each attribute name/attribute value pair
with a comma. The attribute list always follows the classdef or class member keyword, as shown:
classdef (attribute-name = expression, ...) ClassName
Class-Specific Attributes
Some MATLAB classes define additional attributes that you can use only with the class hierarchies
that define these attributes. See the specific documentation for the classes you are using for
information on any additional attributes supported by those classes.
See Also
More About
• “Expressions in Attribute Specifications” on page 6-10
6-7
6 Defining and Organizing Classes
Local functions in classdef files are useful for utility functions that you use only within that file. For
example, this code defines myUtilityFcn outside the classdef block.
classdef MyClass
properties
PropName
end
methods
function obj = method1(val)
adjustedVal = myUtilityFcn(val)
...
end
end
end % End of classdef
When you call method1 of MyClass, the method first uses myUtilityFcn to perform some
preprocessing on the input argument before performing any other actions.
Unlike methods, these functions do not require an instance of the class as an input, but they can take
or return arguments that are instances of the class and access the members of those instances,
including private members. However, if a function inside a class definition file needs direct access to
class members, consider defining the function as a method of the class instead.
See Also
classdef
More About
• “Folders Containing Class Definitions” on page 6-14
6-8
Evaluation of Expressions in Class Definitions
For information on assigning property default values and attribute values, see “Initialize Property
Values” on page 8-13 and “Property Attributes” on page 8-8.
classdef MyClass
% Some attributes are set to logical values
properties (Constant = true)
CnstProp = 2*pi
end
properties
% Static method of this class
Prop1 = MyClass.setupAccount
% Constant property from this class
Prop2 = MyClass.CnstProp
% Function that returns a value
Prop3 = datestr(now)
% A class constructor
Prop4 = AccountManager
end
methods (Static)
function accNum = setupAccount
accNum = randi(9,[1,12]);
end
end
end
MATLAB does not call property set methods when assigning the result of default value expressions to
properties. (See “Property Get and Set Methods” on page 8-38 for information about these special
methods.)
Enumerations that derived from MATLAB types can use expressions to assign a value:
6-9
6 Defining and Organizing Classes
MATLAB evaluates these expressions only once when enumeration members are first accessed.
For attributes values that are logical true or false, class definitions can specify attribute values
using expressions. For example, this assignment makes MyClass sealed (cannot be subclassed) for
versions of MATLAB before R2014b (verLessThan)
classdef (Sealed = verLessThan('matlab','8.4')) MyClass
The expression on the right side of the equal sign (=) must evaluate to true or false. You cannot
use any definitions from the class file in this expression, including any constant properties, static
methods, and local functions.
While you can use conditional expression to set attribute values, doing so can cause the class
definition to change based on external conditions. Ensure that this behavior is consistent with your
class design.
Property definitions allow you to specify default values for properties using any expression that has
no reference to variables. For example, VectorAngle defines a constant property (Rad2Deg) and
uses it in an expression that defines the default value of another property (Angle). The default value
expression also uses a static method (getAngle) defined by the class:
classdef VectorAngle
properties (Constant)
Rad2Deg = 180/pi
end
properties
Angle = VectorAngle.Rad2Deg*VectorAngle.getAngle([1 0],[0 1])
end
methods
function obj = VectorAngle(vx,vy)
obj.Angle = VectorAngle.getAngle(vx,vy);
end
end
methods (Static)
function r = getAngle(vx,vy)
% Calculate angle between 2D vectors
cr = vx(1)*vy(1) + vx(2)*vy(2)/sqrt(vx(1)^2 + vx(2)^2) * ...
sqrt(vy(1)^2 + vy(2)^2);
6-10
Evaluation of Expressions in Class Definitions
r = acos(cr);
end
end
end
You cannot use the input variables to the constructor to define the default value of the Angle
property. For example, this definition for the Angle property is not valid:
properties
Angle = VectorAngle.Rad2Deg*VectorAngle.getAngle(vx,vy)
end
a = VectorAngle([1,0],[0,1])
Expression in class methods execute like expressions in any function. MATLAB evaluates an
expression within the function workspace when the method executes. Therefore, expressions used in
class methods are not considered part of the class definition and are not discussed in this section.
MATLAB evaluates expressions in the context of the class file, so these expressions can access any
functions, static methods, and constant properties of other classes that are on your path at the time
MATLAB initializes the class. Expressions defining property default values can access constant
properties defined in their own class.
After initialization, the values returned by these expressions are part of the class definition and are
constant for all instances of the class. Each instance of the class uses the results of the initial
evaluation of the expressions without re-evaluation.
If you clear a class, then MATLAB reinitializes the class by reevaluating the expressions that are part
of the class definition. (see “Automatic Updates for Modified Classes” on page 5-27)
6-11
6 Defining and Organizing Classes
classdef ContClass
properties
% Assign current date and time
TimeProp = datestr(now)
end
end
classdef ClassExp
properties
ObjProp = ContClass
end
end
When you first use the ClassExp class, MATLAB creates an instance of the ContClass class.
MATLAB initializes both classes at this time. All instances of ClassExp include a copy of this same
instance of ContClass.
a = ClassExp;
a.ObjProp.TimeProp
ans =
08-Oct-2003 17:16:08
The TimeProp property of the ContClass object contains the date and time when MATLAB
initialized the class. Creating additional instances of the ClassExp class shows that the date string
has not changed:
b = ClassExp;
b.ObjProp.TimeProp
ans =
08-Oct-2003 17:16:08
Because this example uses a value class for the contained object, each instance of the ClassExp has
its own copy of the object. For example, suppose that you change the value of the TimeProp property
on the object contained by ClassExp objectb:
b.ObjProp.TimeProp = datestr(now)
ans =
08-Oct-2003 17:22:49
a.ObjProp.TimeProp
ans =
08-Oct-2003 17:16:08
6-12
Evaluation of Expressions in Class Definitions
Creating two instances of the ClassExp class shows that MATLAB created an object when it
initialized the ContClass. MATLAB used a copy of the object’s handle for each instance of the
ClassExp class. Therefore, there is one ContClass object and the ObjProp property of each
ClassExp object contains a copy of its handle.
Create an instance of the ClassExp class and note the time of creation:
a = ClassExp;
a.ObjProp.TimeProp
ans =
08-Oct-2003 17:46:01
Create a second instance of the ClassExp class. The ObjProp contains the handle of the same
object:
b = ClassExp;
b.ObjProp.TimeProp
ans =
08-Oct-2003 17:46:01
b.ObjProp.TimeProp = datestr(now);
b.ObjProp.TimeProp
ans =
08-Oct-2003 17:47:34
The ObjProp property of object b contains a handle to the same object as the ObjProp property of
object a. The value of the TimeProp property has changed on this object as well:
a.ObjProp.TimeProp
ans =
08-Oct-2003 17:47:34
See Also
More About
• “Comparison of Handle and Value Classes” on page 7-2
6-13
6 Defining and Organizing Classes
• Path folders — The folder is on the MATLAB path and the folder name does not begin with an @
character. Use this type of folder when you want multiple classes and functions in one folder. The
entire class definition must be contained in one file.
• Class folders — The folder name begins with an @ character followed by the class name. The
folder is not on the MATLAB path, but its parent folder is on the path. Use this type of folder when
you want to use multiple files for one class definition.
See the path function for information about the MATLAB path.
The name of each class definition file must match the name of the class that is specified with the
classdef keyword. Using a path folder eliminates the need to create a separate class folder for each
class. However, the entire class definition, including all methods, must be contained within a single
file.
.../path_folder/MyClass1.m
.../path_folder/MyClass2.m
.../path_folder/MyClass3.m
6-14
Folders Containing Class Definitions
addpath path_folder
Define only one class per folder. All files have a .m or .p extension. For MATLAB versions R2018a and
later, standalone methods can be live functions with a .mlx extension.
Use a class folder when you want to use more than one file for your class definition. MATLAB treats
any function file in the class folder as a method of the class. Function files can be MATLAB code (.m),
Live Code file format (.mlx), MEX functions (platform dependent extensions), and P-code files (.p).
MATLAB explicitly identifies any file in a class folder as a method of that class. This enables you to
use a more modular approach to authoring methods of your class.
The base name of each file must be a valid MATLAB function name. Valid function names begin with
an alphabetic character and can contain letters, numbers, or underscores. For more information, see
“Methods in Separate Files” on page 9-8.
If a class folder contains a private folder, only the class defined in that folder can access functions
defined in the private folder. Subclasses do not have access to superclass private functions. For
more information on private folders, see “Private Functions”.
If you want a subclass to have access to the private functions of the superclass, define the functions
as protected methods of the superclass. Specify the methods with the Access attribute set to
protected.
If a class defines functions in a private folder that is in a class folder, then MATLAB follows these
precedence rules when dispatching to the private functions versus the methods of the classdef file:
• Using dot notation (obj.methodName), a method defined in the classdef file takes precedence
over the function in the private folder.
• Using function notation (methodName(obj)), a function in a private folder takes precedence
over a method defined in the classdef file.
6-15
6 Defining and Organizing Classes
You cannot put class definitions (classdef file) in private folders because doing so would not meet
the requirements for class or path folders.
A function with the same name as a class in a path folder takes precedence over the class if the
function is in a folder that is earlier on the path. However, a class defined in a class folder (@-folder)
takes precedence over a function of the same name, even if the function is defined in a folder that is
earlier on the path.
For example, consider a path with the following folders and files.
If fldr3/@Foo/Foo.m contains a MATLAB class created before Version 7.6 (that is, the class does
not use the classdef keyword), then fldr4/@Foo/bar.m becomes a method of the Foo class
defined in fldr3/@Foo.
6-16
Folders Containing Class Definitions
In MATLAB Versions 5 through 7, class folders do not shadow other class folders having the same
name, but residing in later path folders. Instead, the class uses the combination of methods from all
class folders having the same name to define the class. This behavior is no longer supported.
For backward compatibility, classes defined in class folders always take precedence over functions
and scripts having the same name. This precedence applies to functions and scripts that come before
these classes on the path.
Suppose that you define two versions of a class named Foo in two folders, fldA and fldB.
fldA/@Foo/Foo.m
fldB/@Foo/Foo.m
Create an instance of class Foo. MATLAB uses fldA/@Foo/Foo.m as the class definition.
a = Foo;
The current folder is always first on the path. Therefore, MATLAB finds fldB/@Foo/Foo.m as the
definition for class Foo.
b = Foo;
MATLAB automatically updates the existing instance, a, to use the new class definition in fldB.
Suppose that you define two versions of a class named Foo in two folders, fldA and fldB, but do not
use a class folder.
fldA/Foo.m
fldB/Foo.m
6-17
6 Defining and Organizing Classes
addpath fldA
Create an instance of class Foo. MATLAB uses fldA/Foo.m as the class definition.
a = Foo;
cd fldB
The current folder is effectively the top of the path. However, MATLAB does not identify fldB/Foo.m
as the definition for class Foo. MATLAB continues to use the original class definition until you clear
the class.
clear Foo
MATLAB automatically updates the existing objects to conform to the class definition in fldB.
Usually, clearing instance variables is unnecessary.
See Also
More About
• “Packages Create Namespaces” on page 6-21
• “Automatic Updates for Modified Classes” on page 5-27
• “Live Code File Format (.mlx)”
• “Call MEX Functions”
• “Using MEX Functions for MATLAB Class Methods”
• “Security Considerations to Protect Your Source Code”
6-18
Class Precedence
Class Precedence
In this section...
“Use of Class Precedence” on page 6-19
“Why Mark Classes as Inferior” on page 6-19
“InferiorClasses Attribute” on page 6-19
InferiorClasses Attribute
Specify the relative precedence of user-defined classes using the class InferiorClasses attribute.
To specify classes that are inferior to the class you are defining, assign a cell array of class
meta.class objects to this attribute.
For example, the following classdef declares that MyClass is dominant over ClassName1 and
ClassName2.
The ? operator combined with a class name creates a meta.class object. See metaclass.
The following MATLAB classes are always inferior to classes defined using the classdef syntax and
cannot be used in this list.
double, single, int64, uint64, int32, uint32, int16, uint16, int8, uint8, char, string,
logical, cell, struct, and function_handle.
6-19
6 Defining and Organizing Classes
Dominant Class
MATLAB uses class dominance when evaluating expressions involving objects of more than one class.
The dominant class determines:
• Which class method to call when more than one class defines methods with the same names.
• The class of arrays that are formed by combining objects of different classes, assuming MATLAB
can convert the inferior objects to the dominant class.
No Attribute Inheritance
Subclasses do not inherit a superclass InferiorClasses attribute. Only classes specified in the
subclass InferiorClasses attribute are inferior to subclass objects.
See Also
More About
• “Class Precedence and MATLAB Path” on page 6-16
6-20
Packages Create Namespaces
Package Folders
Packages are special folders that can contain class folders, function, and class definition files, and
other packages. The names of classes and functions are scoped to the package folder. A package is a
namespace within which names must be unique. Function and class names must be unique only
within the package. Using a package provides a means to organize classes and functions. Packages
also enable you to reuse the names of classes and functions in different packages.
Note Packages are not supported for classes created before MATLAB Version 7.6 (that is, classes
that do not use classdef).
The parent of the top-level package folder must be on the MATLAB path.
Internal Packages
MathWorks® reserves the use of packages named internal for utility functions used by internal
MATLAB code. Functions that belong to an internal package are intended for MathWorks use only.
6-21
6 Defining and Organizing Classes
Using functions or classes that belong to an internal package is discouraged. These functions and
classes are not guaranteed to work in a consistent manner from one release to the next. Any of these
functions and classes might be removed from the MATLAB software in any subsequent release
without notice and without documentation in the product release notes.
+mypack/pkfcn.m
z = mypack.pkfcn(x,y);
Definitions do not use the package prefix. For example, the function definition line of the pkfcn.m
function would include only the function name:
function z = pkfcn(x,y)
classdef myClass
obj = mypack.myClass(arg1,arg2,...);
Calling class methods does not require the package name because you have an object of the class.
You can use dot or function notation:
obj.myMethod(arg)
myMethod(obj,arg)
A static method requires the full class name, which includes the package name:
mypack.myClass.stMethod(arg)
obj = mypack.MyClass;
This section shows you how to access various package members from outside a package. Suppose
that you have a package mypack with the following contents:
+mypack
+mypack/myFcn.m
6-22
Packages Create Namespaces
+mypack/@MyFirstClass
+mypack/@MyFirstClass/myFcn.m
+mypack/@MyFirstClass/otherFcn.m
+mypack/@MyFirstClass/MyFirstClass.m
+mypack/@MySecondClass
+mypack/@MySecondClass/MySecondClass.m
+mypack/+mysubpack
+mypack/+mysubpack/myFcn.m
mypack.myFcn(arg)
obj1 = mypack.MyFirstClass;
obj2 = mypack.MySecondClass(arg);
mypack.mysubpack.myFcn(arg1,arg2);
If mypack.MyFirstClass has a method called myFcn, call it like any method call on an object:
obj = mypack.MyFirstClass;
myFcn(obj,arg);
If mypack.MyFirstClass has a property called MyProp, assign it using dot notation and the object:
obj = mypack.MyFirstClass;
obj.MyProp = x;
Package members remain scoped to the package. Always refer to the package members using the
package name. Alternatively, import the package into the function in which you call the package
member, see “Import Classes” on page 6-25.
Package folders do not shadow other package folders that are positioned later on the path, unlike
classes, which do shadow other classes. If two or more packages have the same name, MATLAB
treats them all as one package. If redundantly named packages in different path folders define the
same function name, then MATLAB finds only one of these functions.
Suppose a package and a class have the same name. For example:
fldr_1/+foo
fldr_2/@foo/foo.m
A call to which foo returns the path to the executable class constructor:
6-23
6 Defining and Organizing Classes
A function and a package can have the same name. However, a package name by itself is not an
identifier. Therefore, if a redundant name occurs alone, it identifies the function. Executing a package
name alone returns an error.
In cases where a package and a class have the same name, a package function takes precedence over
a static method. For example, path folder fldrA contains a package function and path folder fldrB
contains a class static method:
fldrA/+foo/bar.m % bar is a function in package foo
fldrB/@foo/bar.m % bar is a static method of class foo
which foo.bar
In cases where the same path folder contains both package and class folders with the same name, the
package function takes precedence over the static method.
fldr/@foo/bar.m % bar is a static method of class foo
fldr/+foo/bar.m % bar is a function in package foo
which foo.bar
fldr/+foo/bar.m
If a path folder fldr contains a classdef file foo that defines a static method bar and the same
folder contains a package +foo that contains a package function bar.
fldr/foo.m % bar is a static method of class foo
fldr/+foo/bar.m % bar is a function in package foo
which foo.bar
fldr/+foo/bar.m
See Also
More About
• “Folders Containing Class Definitions” on page 6-14
• “Class Precedence” on page 6-19
6-24
Import Classes
Import Classes
In this section...
“Syntax for Importing Classes” on page 6-25
“Import Static Methods” on page 6-25
“Import Package Functions” on page 6-25
“Package Function and Class Method Name Conflict” on page 6-26
“Clearing Import List” on page 6-26
function myFunc
import pkg.*
obj1 = MyClass1(arg,...); % call pkg.MyClass1 constructor
obj2 = MyClass2(arg,...); % call pkg.MyClass2 constructor
a = pkgFunction(); % call package function named pkgFunction
end
function myFunc
import pkg.MyClass.MyStaticMethod
MyStaticMethod(arg,...); % call static method
end
function myFunc
import pkg.pkgFunction
pkgFunction(arg,...); % call imported package function
end
6-25
6 Defining and Organizing Classes
import pkg.*
myobj = pkg.MyClass;
timedata(myobj)
A call to timedata finds the package function, not the class method because MATLAB applies the
import and finds pkg.timedata first. Do not use a package in cases where you have name conflicts
and plan to import the package.
clear import
See Also
import
More About
• “Packages Create Namespaces” on page 6-21
6-26
Creating and Managing Class Aliases
The recommended process for creating and maintaining alias files is to use functions to automate the
process. To create a class alias definition, the function must:
Run the function from the same folder that contains the new class definition. For example, a class
named FirstName is defined in a folder named Work. Update the name of the class from FirstName
to SecondName using a function called createAliasFile with this folder structure:
function createAliasFile
fileMgr = matlab.alias.AliasFileManager;
addAlias(fileMgr,NewName="SecondName",OldNames="FirstName");
writeAliasFile(fileMgr);
end
The FirstName class file is not needed to create the alias. In fact, you must remove the original
definition from the path so that MATLAB finds the newer alias instead of the older definition.
The final folder structure after running createAliasFile looks like this:
MATLAB recognizes both FirstName and SecondName as the same class as long as SecondName.m
and the associated alias resources folder are in the same folder on the MATLAB path.
6-27
6 Defining and Organizing Classes
You can define multiple aliases for the same class. When doing so, you must pass in all of the old
aliases to the addAlias method. The aliases must be listed in order from newest to oldest. Keeping
the original function used to define the alias can help you avoid errors by maintaining a record of the
alias definitions. addAlias returns an error if all of the previous aliases are not included as part of
the OldNames input argument.
Renaming a Package
Renaming the entire contents of a package in one step is not supported. To rename a package, you
must define aliases for all the classes in the package. For an example, see “Rename Package of Two
Classes”.
Note You cannot create aliases for package functions. To rename a package that contains functions,
keep the old package in place and redefine the package functions to redirect to the functions in the
new package.
• Create a meta.class instance for the class you want to investigate. The Aliases property of
meta.class returns all of the defined class aliases in a string array, in order from newest to
oldest. For more information, see meta.class.
• Create a matlab.alias.AliasFileManager instance with the location input argument,
where location points to the folder that contains the resources folder of the class alias
definition file. The Aliases property of the matlab.alias.AliasFileManager instance
returns an array of all alias definitions in that given definition file. For an example of accessing the
Aliases property, see “Rename Package of Two Classes”.
Note that reading in an existing alias definition file does not validate that the current class names
in that file exist.
The alias definition enables MATLAB to recognize both names as the same class. Including or
creating an alias definition in your new code ensures backward compatibility:
• New code can work with code that uses old class names because the old names are recognized as
aliases.
• New code can load and work with MAT-files containing serialized objects that were created using
the old names.
6-28
Creating and Managing Class Aliases
When serializing objects, using the oldest defined alias for the objects allows the greatest range of
compatibility. Objects serialized using the oldest alias can be used by any code back to the original
definition and any future code that maintains the full alias definition. Deleting alias definitions or
deleting old names from existing definitions is not recommended because backward and forward
compatibility can be limited.
See Also
matlab.alias.AliasFileManager
6-29
7
In this section...
“Basic Difference” on page 7-2
“Behavior of MATLAB Built-In Classes” on page 7-2
“User-Defined Value Classes” on page 7-3
“User-Defined Handle Classes” on page 7-4
“Determining Equality of Objects” on page 7-6
“Functionality Supported by Handle Classes” on page 7-7
Basic Difference
A value class constructor returns an object that is associated with the variable to which it is assigned.
If you reassign this variable, MATLAB creates an independent copy of the original object. If you pass
this variable to a function to modify it, the function must return the modified object as an output
argument. For information on value-class behavior, see “Avoid Unnecessary Copies of Data”.
A handle class constructor returns a handle object that is a reference to the object created. You can
assign the handle object to multiple variables or pass it to functions without causing MATLAB to
make a copy of the original object. A function that modifies a handle object passed as an input
argument does not need to return the object.
All handle classes are derived from the abstract handle class.
By default, MATLAB classes are value classes. The following definition creates a value class named
MyValueClass:
classdef MyValueClass
...
end
To create a handle class, derive the class from the handle class.
classdef MyHandleClass < handle
...
end
7-2
Comparison of Handle and Value Classes
b
7
MATLAB graphics objects are implemented as handle objects because they represent visual elements.
For example, create a graphics line object and copy its handle to another variable. Both variables
refer to the same line object.
x = 1:10; y = sin(x);
l1 = line(x,y);
l2 = l1;
Set the properties of the line object using either copy of the handle.
set(l2,'Color','red')
set(l1,'Color','green')
get(l2,'Color')
ans =
0 1 0
Calling the delete function on the l2 handle destroys the line object. If you attempt to set the
Color property on the line l1, the set function returns an error.
delete(l2)
set(l1,'Color','blue')
If you delete the object by deleting any one of the existing handles, all copies are now invalid because
you deleted the single object to which all handles refer.
Deleting a handle object is not the same as clearing the handle variable. In the graphics object
hierarchy, the parent of the object holds a reference to the object. For example, the parent axes hold
a reference to the line object referred to by l1 and l2. If you clear both variables from the
workspace, the object still exists.
For more information on the behavior of handle objects, see “Handle Object Behavior” on page 1-7.
Value objects are always associated with one workspace or temporary variable. Value objects go out
of scope when their variable goes out of scope or is cleared. There are no references to value objects,
only copies that are independent objects.
Here is a value class that stores a value in its Number property. The default property value is the
number 1.
7-3
7 Value or Handle Class — Which to Use
classdef NumValue
properties
Number = 1
end
end
a =
Number: 1
b =
Number: 1
The variables a and b are independent. Changing the value of the Number property of a does not
affect the Number property of b.
a.Number = 7
a =
Number: 7
b =
Number: 1
When you pass a value object to a function, MATLAB creates a copy of that object in the function
workspace. Because copies of value objects are independent, the function does not modify the object
in the caller’s workspace. Therefore, functions that modify value objects must return the modified
object to be reassigned in the caller’s workspace.
7-4
Comparison of Handle and Value Classes
object properties. The copy refers to the same object as the original handle. If you change a property
value on the original object, the copied handle references the same change.
Here is a handle class that stores a value in its Number property. The default property value is the
number 1.
a = NumHandle
a =
Number: 1
b = a
b =
Number: 1
The variables a and b refer to the same underlying object. Changing the value of the Number
property of a also changes the Number property of b. That is, a and b refer to the same object.
a.Number = 7
a =
Number: 7
b =
Number: 7
When you pass a handle object to a function, MATLAB creates a copy of the handle in the function
workspace. Because copies of handles reference the same underlying object, functions that modify
the handle object effectively modify the object in the caller’s workspace as well. Therefore, it is not
7-5
7 Value or Handle Class — Which to Use
necessary for functions that modify handle objects passed as input arguments to return the modified
object to the caller.
Deleting Handles
You can destroy handle objects by explicitly calling the handle delete method. Deleting the handle of
a handle class object makes all handles invalid. For example:
a = NumHandle;
b = a;
delete(a)
b.Number
Calling delete on a handle object invokes the destructor function or functions for that object. See
“Handle Class Destructor” on page 7-13 for more information.
For information on the differences between initializing properties to default values in the properties
block and initializing properties from within the constructor, see “Initialize Property Values” on page
8-13 and “Create and Initialize Object Arrays” on page 10-2.
Equality for handle objects means that the handle variables refer to the same object. You also can
identify handle variables that refer to different objects of the same class that have the same state.
To determine if value objects are the same size and their contents are of equal value, use isequal.
For example, use the previously defined NumValue class to create two instances and test for equality:
a = NumValue;
b = NumValue;
isequal(a,b)
ans =
a and b are independent and therefore are not the same object. However each represents the same
value.
If you change the value represented by a value object, the objects are no longer equal.
a = NumValue;
b = NumValue;
b.Number = 7;
isequal(a,b)
7-6
Comparison of Handle and Value Classes
ans =
Handle objects inherit an eq method from the handle base class. You can use == and isequal to
test for two different relationships among handle objects:
• The handles refer to the same object: == and isequal return true.
• The handles refer to objects of the same class that have the same values, but are not the same
objects — only isequal returns true.
Use the previously defined NumHandle class to create an object and copy the handle.
a = NumHandle;
b = a;
a == b
ans =
isequal(a,b)
ans =
Create two instances of the NumHandle class using the default values.
a = NumHandle;
b = NumHandle;
a == b
ans =
isequal(a,b)
ans =
7-7
7 Value or Handle Class — Which to Use
See “The Handle Superclass” on page 7-11 for more information on the handle class and its
methods.
See Also
Related Examples
• “Which Kind of Class to Use” on page 7-9
• “Implement Copy for Handle Classes” on page 7-30
• “Handle Object Behavior” on page 1-7
7-8
Which Kind of Class to Use
“Representing Polynomials with Classes” on page 19-2 and “Representing Structured Data with
Classes” on page 3-14 provides examples of value classes.
Handle classes enable you to create objects that more than one function or object can share. Handle
objects allow more complex interactions among objects because they allow objects to reference each
other.
“Implementing Linked Lists with Classes” on page 3-23 and “Developing Classes That Work Together”
on page 3-6 provides examples of a handle class.
A value class is suitable because you can copy a polynomial object and have two objects that are
identical representations of the same polynomial. For an example of value classes, see “Subclasses of
MATLAB Built-In Types” on page 12-40.
For information on MATLAB pass-by-value semantics, see “Avoid Unnecessary Copies of Data”.
• No two instances of a class can have the same state, making it impossible to have exact copies.
For example:
• A copy of a graphics object (such as a line) has a different position in its parents list of children
than the object from which it was copied. Therefore, the two objects are not identical.
7-9
7 Value or Handle Class — Which to Use
• Nodes in lists or trees having specific connectivity to other nodes — no two nodes can have the
same connectivity.
• The class represents physical and unique objects like serial ports and printers.
• The class represents visible objects like graphics components.
• The class defines events and notifies listeners when an event occurs (notify is a handle class
method).
• The class creates listeners by calling the handle class addlistener method.
• The class subclasses the dynamicprops class (a subclass of handle) so that instances can define
dynamic properties.
• The class subclasses the matlab.mixin.SetGet class (a subclass of handle) so that it can
implement a graphics object style set/get interface to access property values.
• You want to create a singleton class or a class in which you track the number of instances from
within the constructor.
• Instances of a class cannot share state, such as nodes in a linked list.
See Also
Related Examples
• “Handle Compatible Classes” on page 12-31
7-10
The Handle Superclass
To add both handle behavior and specific functionality to your class, derive your class from these
handle classes:
For information on how to define subclasses, see “Design Subclass Constructors” on page 12-7.
List the methods of a class by passing the class name to the methods function:
methods('handle')
addlistener findobj gt lt
delete findprop isvalid ne
eq ge le notify
7-11
7 Value or Handle Class — Which to Use
Relational Methods
TF = eq(H1,H2)
TF = ne(H1,H2)
TF = lt(H1,H2)
TF = le(H1,H2)
TF = gt(H1,H2)
TF = ge(H1,H2)
The handle class overloads these functions to support equality tests and sorting on handles. For each
pair of input arrays, these functions return a logical array of the same size. Each element is an
element-wise equality or comparison test result. The input arrays must be the same size or one (or
both) can be scalar. The method performs scalar expansion as required. For more information on
handle class relational methods, see relationaloperators.
B = isvalid(H)
B is a logical array in which each element is true if, and only if, the corresponding element of H is a
valid handle. B is always the same size as H.
When MATLAB destroys an object, it also destroys values stored in the properties of the object.
MATLAB frees computer memory associated with the object for use by MATLAB or the operating
system.
You do not need to free memory in handle classes. However, there can be other operations that you
want to perform when destroying an object. For example, closing a file or shutting down an external
program that the object constructor started. Define a delete method in your handle subclass for
these purposes.
See Also
Related Examples
• “Comparison of Handle and Value Classes” on page 7-2
7-12
Handle Class Destructor
Basic Knowledge
Class destructor – a method named delete that MATLAB calls implicitly before destroying an object
of a handle class. Also, user-defined code can call delete explicitly to destroy an object.
Nondestructor – a method named delete that does not meet the syntax requirements of a valid
destructor. Therefore, MATLAB does not call this method implicitly when destroying handle objects. A
method named delete in a value class is not a destructor. A method named delete in a value class
that sets the HandleCompatible attribute to true is not a destructor.
• Must define one, scalar input argument, which is an object of the class.
• Must not define output arguments
• Cannot be Sealed, Static, or Abstract
• Cannot use arguments blocks for input argument validation.
7-13
7 Value or Handle Class — Which to Use
MATLAB does not call a noncompliant delete method when destroying objects of the class. A
noncompliant delete method can prevent the destruction of the object by shadowing the handle
class delete method.
A delete method defined by a value class that is handle compatible is not a destructor, even if the
delete method is inherited by a handle subclass. For information on handle compatible classes, see
“Handle Compatible Classes” on page 12-31.
methods
function delete(obj)
% obj is always scalar
...
end
end
MATLAB calls the delete method separately for each element in an array. Therefore, a delete
method is passed only one scalar argument with each invocation.
Calling delete on a deleted handle should not error and can take no action. This design enables
delete to work on object arrays containing a mix of valid and invalid objects.
A delete method can access properties of the object being deleted. MATLAB does not destroy these
properties until after the delete methods for the class of the object and all superclasses finish
executing.
If a delete method creates new variables that contain a handle to the object being deleted, those
handles are invalid. After the delete method finishes execution, handles to the deleted object in any
variables in any workspace are invalid.
The isvalid method returns false for the handle object within the delete method because object
destruction begins when the method is called.
MATLAB calls delete methods in the inverse of the construction order. That is, MATLAB invokes
subclass delete methods before superclass delete methods.
If a superclass expects a property to be managed by subclasses, then the superclass should not
access that property in its delete method. For example, if a subclass uses an inherited abstract
property to store an object handle, then the subclass should destroy this object in its delete method,
but the superclass should not access that property in its delete method.
7-14
Handle Class Destructor
For example, the PartialObject class delete method determines if the Data property is empty
before accessing the data this property contains. If an error occurs while assigning the constructor
argument to the Name property, MATLAB passes the partially constructed object to delete.
An error occurs if you call the constructor with a char vector, instead of the required cell array:
obj = PartialObject('Test')
MATLAB passes the partially constructed object to the delete method. The constructor did not set
the value of the Data property because the error occurred when setting the Name property.
Data is empty
Error setting 'Name' property of 'PartialObject' class:
...
If an error occurs during the construction of a handle class, MATLAB calls the class destructor on the
object along with the destructors for any objects contained in properties and any initialized base
classes.
7-15
7 Value or Handle Class — Which to Use
For example, suppose that a method opens a file for writing and you want to close the file in your
delete method. The delete method can call fclose on a file identifier that the object stores in its
FileID property:
function delete(obj)
fclose(obj.FileID);
end
Classes cannot define a valid destructor that is Sealed. MATLAB returns an error when you attempt
to instantiate a class that defines a Sealed delete method.
Normally, declaring a method as Sealed prevents subclasses from overriding that method. However,
a Sealed method named delete that is not a valid destructor does not prevent a subclass from
defining its own destructor.
For example, if a superclass defines a method named delete that is not a valid destructor, but is
Sealed, then subclasses:
Heterogeneous class hierarchies require that all methods to which heterogeneous arrays are passed
must be sealed. However, the rule does not apply to class destructor methods. Because destructor
methods cannot be sealed, you can define a valid destructor in a heterogeneous hierarchy that is not
sealed, but does function as a destructor.
Object Lifecycle
MATLAB invokes the delete method when the lifecycle of an object ends. The lifecycle of an object
ends when the object is:
Inside a Function
The lifecycle of an object referenced by a local variable or input argument exists from the time the
variable is assigned until the time it is reassigned, cleared, or no longer referenced within that
function or in any handle array.
7-16
Handle Class Destructor
A variable goes out of scope when you explicitly clear it or when its function ends. When a variable
goes out of scope and its value belongs to a handle class that defines a delete method, MATLAB
calls that method. MATLAB defines no ordering among variables in a function. Do not assume that
MATLAB destroys one value before another value when the same function contains multiple values.
MATLAB invokes the delete methods in the following sequence when destroying an object:
MATLAB invokes the delete methods of superclasses at the same level in the hierarchy in the order
specified in the class definition. For example, the following class definition specifies supclass1
before supclass2. MATLAB calls the delete method of supclass1 before the delete method of
supclass2.
After calling each delete method, MATLAB destroys the property values belonging exclusively to the
class whose method was called. However, if the property values contain handle objects that are still
referenced outside the scope of the class, calling delete on the containing object does not delete the
property handle objects themselves. They can still be accessed by other existing references.
Superclass delete methods cannot call methods or access properties belonging to a subclass.
Consider a set of objects that reference other objects of the set such that the references form a cyclic
graph. In this case, MATLAB:
• Destroys the objects if they are referenced only within the cycle
• Does not destroy the objects as long as there is an external reference to any of the objects from a
MATLAB variable outside the cycle
MATLAB destroys the objects in the reverse of the order of construction. for more information, see
“Handle Object During delete Method Execution” on page 7-14.
delete(obj)
A class can prevent explicit destruction of an object by setting its delete method Access attribute
to private. However, a method of the class can call the private delete method.
If the class delete method Access attribute is protected, only methods of the class and of
subclasses can explicitly delete objects of that class.
However, when an object lifecycle ends, MATLAB calls the object’s delete method when destroying
the object regardless of the method’s Access attribute.
7-17
7 Value or Handle Class — Which to Use
Class destructor behavior differs from the normal behavior of an overridden method. MATLAB
executes each delete method of each superclass upon destruction, even if that delete method is
not public.
When you explicitly call an object’s delete method, MATLAB checks the delete method Access
attribute in the class defining the object, but not in the superclasses of the object. A superclass with a
private delete method cannot prevent the destruction of subclass objects.
Declaring a private delete method makes most sense for sealed classes. In the case where classes are
not sealed, subclasses can define their own delete methods with public access. MATLAB calls a
private superclass delete method as a result of an explicit call to a public subclass delete method.
For example, if the superclass implements a Sealed method named delete that is not a valid
destructor, then MATLAB does not allow subclasses to override this method.
If you cannot avoid external references to MATLAB objects, explicitly break the cyclic reference by
destroying the objects in MATLAB.
The following section describes how to manage this situation when using Java objects that reference
MATLAB objects.
Java does not support the object destructors that MATLAB objects use. Therefore, it is important to
manage the lifecycle of all objects used in applications that include both Java and MATLAB objects.
Java objects that hold references to MATLAB objects can prevent deletion of the MATLAB objects. In
these cases, MATLAB does not call the handle object delete method even when there is no handle
variable referring to that object. To ensure your delete method executes, call delete on the object
explicitly before the handle variable goes out of scope.
Problems can occur when you define callbacks for Java objects that reference MATLAB objects.
7-18
Handle Class Destructor
function testDestructor
cwj = CallbackWithJava
...
end
testDestructor
cwj =
The handle variable, cwj, exists only in the function workspace. However, MATLAB does not call the
class delete method when the function ends. The com.mathworks.jmi.Callback object still
exists and holds a reference to the object of the CallbackWithJava class, which prevents
destruction of the MATLAB object.
clear classes
Warning: Objects of 'CallbackWithJava' class exist. Cannot clear this class or
any of its superclasses.
To avoid causing inaccessible objects, call delete explicitly before losing the handle to the MATLAB
object.
function testDestructor
cwj = CallbackWithJava
...
delete(cwj)
end
MATLAB applications that use Java or other external-language objects should manage the lifecycle of
the objects involved. A typical user interface application references Java objects from MATLAB
objects and creates callbacks on Java objects that reference MATLAB objects.
7-19
7 Value or Handle Class — Which to Use
• Explicitly call delete on the MATLAB objects when they are no longer needed
• Unregister the Java object callbacks that reference MATLAB objects
• Use intermediate handle objects that reference both the Java callbacks and the MATLAB objects.
See Also
More About
• “Handle Object Behavior” on page 1-7
7-20
Find Handle Objects and Properties
function HM = findobj(H,<conditions>)
The findobj method returns an array of handles matching the conditions specified. You can use
regular expressions with findobj. For more information, see regexp.
function mp = findprop(h,'PropertyName')
The property can also be a dynamic property created by the addprop method of the dynamicprops
class.
Use the returned meta.property object to obtain information about the property, such as the
settings of any of its attributes. For example, the following statements determine that the setting of
the AccountStatus property Dependent attribute is false.
ba = BankAccount(007,50,'open');
mp = findprop(ba,'AccountStatus');
mp.Dependent
ans =
0
See Also
handle
Related Examples
• “Class Metadata” on page 16-2
7-21
7 Value or Handle Class — Which to Use
You can add set and get functionality to your class by deriving from one of these classes:
• matlab.mixin.SetGet — use when you want support for case-insensitive, partial property name
matching. Deriving from matlab.mixin.SetGet does not affect the exact property name
required by the use of dot notation reference to properties.
• matlab.mixin.SetGetExactNames — use when you want to support only case-sensitive full
property name matching.
Note The set and get methods referred to in this section are different from property set access and
property get access methods. See “Property Get and Set Methods” on page 8-38 for information on
property access methods.
Subclass Syntax
Use the abstract class matlab.mixin.SetGet or matlab.mixin.SetGetExactNames as a
superclass:
classdef MyClass < matlab.mixin.SetGet
...
end
If you specify an array of handles with a single property name, get returns the property value for
each object as a cell array of values:
7-22
Implement Set/Get Interface for Properties
CV = get(H,'PropertyName');
If you specify a cell array of char vector property names and an array of handles, get returns a cell
array of property values. Each row in the cell corresponds to an object in the handle array. Each
column in the cell corresponds to a property name.
props = {'PropertyName1','PropertyName2'};
CV = get(H,props);
If you specify a handle array, but no property names, get returns an array of type struct in which
each structure in the array corresponds to an object in H. Each field in each structure corresponds to
a property defined by the class of H. The value of each field is the value of the corresponding property.
SV = get(H);
For an example, see “Using get with Arrays of Handles” on page 7-25.
set(H,'PropertyName',PropertyValue)
You can pass a cell array of property names and a cell array of property values to set:
props = {'PropertyName1','PropertyName2'};
vals = {Property1Value,Property2Value};
set(H,props,vals)
If length(H) is greater than one, then the property value cell array (vals) can have values for each
property in each object. For example, suppose length(H) is 2 (two object handles). You want to
assign two property values on each object:
props = {'PropertyName1','PropertyName2'};
vals = {Property11Value,Property12Value;Property21Value,Property22Value};
set(H,props,vals))
If you specify a scalar handle, but no property names, set returns a struct with one field for each
property in the class of H. Each field contains an empty cell array.
SV = set(h);
Tip You can use any combination of property name/property value cell arrays, structure arrays (with
the field names as property names and field values as property values), and cell arrays in one call to
set.
7-23
7 Value or Handle Class — Which to Use
h = LineType('--','*');
Query the value of any object property using the inherited get method:
get(h,'Marker')
ans =
'*'
Set the value of any property using the inherited set method:
set(h,'Marker','Q')
MATLAB calls property access methods (set.Style or set.Marker in the LineType class) when
you use the set and get methods.
set(h,'Style','-.-')
7-24
Implement Set/Get Interface for Properties
For more information on property access methods, see “Property Get and Set Methods” on page 8-
38
Return a struct containing object properties and their current values using get:
h = LineType('--','*');
SV = get(h)
SV =
Style: '--'
Marker: '*'
Units: 'points'
Return a struct containing the properties that have public SetAccess using set:
S = set(h)
S =
Style: {}
Marker: {}
The LineType class defines the Units property with SetAccess = protected. Therefore, S =
set(h) does not create a field for Units in S.
set cannot return possible values for properties that have nonpublic set access.
H = [LineType('..','z'),LineType('--','q')]
H =
Style
Marker
Units
When H is an array of handles, get returns a (length(H)-by-1) cell array of property values:
CV = get(H,'Style')
CV =
7-25
7 Value or Handle Class — Which to Use
{'..'}
{'--'}
When H is an array of handles and you do not specify a property name, get returns a struct array
containing fields with names corresponding to property-names. Assign the output of get to a variable
when H is not scalar.
SV = get(H)
SV =
Get the value of the Marker property from the second array element in the SV array of structures:
SV(2).Marker
ans =
'q'
You can pass an array of handles, a cell array of property names, and a cell array of property values
to set. The property value cell array must have one row of property values for each object in H. Each
row must have a value for each property in the property name array:
H = [LineType('..','z'),LineType('--','q')];
set(H,{'Style','Marker'},{'..','o';'--','x'})
H(1)
ans =
Style: '..'
Marker: 'o'
Units: 'points
H(2)
ans =
Style: '--'
Marker: 'x'
Units: 'points'
Customize the way property lists display by redefining the following methods in your subclass:
7-26
Implement Set/Get Interface for Properties
• setdisp — When you call set with no output argument and a single scalar handle input, set
calls setdisp to determine how to display the property list.
• getdisp — When you call get with no output argument and a single scalar handle input, get
calls getdisp to determine how to display the property list.
The inherited set and get methods can resolve inexact property names when there are no
ambiguities resulting from inexact name strings. When a partial property name is ambiguous because
the name matches more than one property, the PartialMatchPriority attribute value can
determine which property MATLAB matches.
For example, in this class the Verbosity property has a higher priority for name matching than the
Version property.
Calling the set method with the potentially ambiguous inexact name Ver sets the Verbosity
property because of its higher relative priority. Without setting the PartialMatchPriority
attribute, the ambiguous name would cause an error.
a = MyClass;
set(a,"Ver",10)
disp(a)
Verbosity: 10
Version: []
v = get(a,"Ver")
v =
10
7-27
7 Value or Handle Class — Which to Use
A full name match with nonmatching case takes precedence over a partial match with a higher
priority property. For example, this class defines the BaseLine property with a priority of 1 (the
default) and a Base property with a priority of 2 (lower than 1).
classdef MyClass < matlab.mixin.SetGet
properties
BaseLine
end
properties (PartialMatchPriority = 2)
Base
end
end
Calling the set method with the string base sets the Base property. BaseLine has a higher priority,
but the full name match with incorrect case takes precedence.
a = MyClass;
set(a,"base",-2)
disp(a)
BaseLine: []
Base: -2
You can use the PartialMatchPriority attribute to avoid introducing code incompatibilities when
adding a new property. For example, this class enables the set and get methods to refer to the
Distance property with the string Dis because the DiscreteSamples property has a lower
priority.
classdef Planet < matlab.mixin.SetGet
% Version 1.0
properties
Distance
end
properties(PartialMatchPriority = 2)
DiscreteSamples
end
end
Version 2.0 of the class introduces a property named Discontinuities. To prevent the possibility of
causing an ambiguous partial property name in existing code, use PartialMatchPriority to set
the priority of Discontinuities lower than that of previously existing properties.
classdef Planet < matlab.mixin.SetGet
% Version 2.0
properties
Diameter;
NumMoons = 0
ApparentMagnitude;
DistanceFromSun;
end
properties(PartialMatchPriority = 2)
DiscreteSamples;
7-28
Implement Set/Get Interface for Properties
end
properties(PartialMatchPriority = 3)
Discontinuities = false;
end
end
For version 1.0 of the Planet class, this call to the set method was not ambiguous.
p = Planet;
set(p,"Disc",true)
However, with the introduction of the Discontinuities property, the string Disc becomes
ambiguous. By giving the Discontinuities property a lower priority, the string Disc continues to
match the DiscreteSamples property.
Note When writing reusable code, using complete, case-sensitive property names avoids
ambiguities, prevents incompatibilities with subsequent software releases, and produces more
readable code.
See Also
set | get | matlab.mixin.SetGet | matlab.mixin.SetGetExactNames
More About
• “Ways to Use Properties” on page 8-2
7-29
7 Value or Handle Class — Which to Use
In this section...
“Copy Method for Handle Classes” on page 7-30
“Customize Copy Operation” on page 7-31
“Copy Properties That Contain Handles” on page 7-32
“Exclude Properties from Copy” on page 7-33
Create an object of the CopyObj class and assign the handle of a line object to the property Prop.
a = CopyObj;
a.Prop = line;
b = copy(a);
a == b
ans =
logical
However, the line object referred to by a.Prop has not been copied. The handle contained in
a.Prop refers to the same object as the handle contained in b.Prop.
a.Prop == b.Prop
ans =
logical
For more detailed information on the behavior of the copy operation, see copy.
7-30
Implement Copy for Handle Classes
• copy — Sealed method that defines the interface for copying objects
• copyElement — Protected method that subclasses can override to customize object copy
operations for the subclass
The matlab.mixin.Copyable copy method, calls the copyElement method. Your subclass
customizes the copy operation by defining its own version of copyElement.
The default implementation of copyElement makes shallow copies of all the nondependent
properties. copyElement copies each property value and assigns it to the new (copied) property. If a
property value is a handle object, copyElement copies the handle, but not the underlying data.
To implement different copy behavior for different properties, override copyElement. For example,
the copyElement method of the SpecializedCopy class:
a = SpecializedCopy;
a.Prop1 = 7
a =
Prop1: 7
Prop2: '17-Feb-2015 17:51:23'
b = copy(a)
b =
7-31
7 Value or Handle Class — Which to Use
Prop1: 7
Prop2: '17-Feb-2015 17:51:58'
The copy (object b) has the same value for Prop1, but the subclass copyElement method assigned a
new value to Prop2. Notice the different timestamp.
Suppose that you define a class that stores a handle in an object property. You want to be able to copy
objects of the class and want each copy of an object to refer to a new handle object. Customize the
class copy behavior using these steps:
The “HandleCopy” on page 7-32 class customizes copy operations for the property that contains a
handle object. The “ColorProp” on page 7-33 class defines the handle object to assign to Prop2:
Make a copy of the object using the copy method inherited from matlab.mixin.Copyable:
b = copy(a);
Demonstrate that the handle objects contained by objects a and b are independent. Changing the
value on object a does not affect object b:
a.Prop2.Color = 'red';
b.Prop2.Color
ans =
blue
HandleCopy
The HandleCopy class customizes the copy operation for objects of this class.
classdef HandleCopy < matlab.mixin.Copyable
properties
7-32
Implement Copy for Handle Classes
ColorProp
The ColorProp class defines a color by assigning an RGB value to its Color property.
classdef ColorProp < handle
properties
Color = 'blue';
end
end
For classes that derive from matlab.mixin.Copyable, the default implementation of copyElement
honors the NonCopyable attribute. Therefore, if a property has its NonCopyable attribute set to
true, then copyElement does not copy the value of that property. If you override copyElement in
your subclass, you can choose how to use the NonCopyable attribute.
7-33
7 Value or Handle Class — Which to Use
Default Values
If a property that is not copyable has a default value assigned in the class definition, the copy
operation assigns the default value to the property. For example, the CopiedClass assigns a default
value to Prop2.
a = CopiedClass;
a.Prop1 = 7
a =
Prop1: 7
Prop2: '17-Feb-2015 15:19:34'
b = copy(a)
b =
Prop1: []
Prop2: '17-Feb-2015 15:19:34'
In the copy b, the value of Prop1 is not copied. The value of Prop2 is set to its default value, which
MATLAB determined when first loading the class. The timestamp does not change.
Subclasses of the dynamicprops class allow you to add properties to an object of the class. When a
class derived from dynamicprops is also a subclass of matlab.mixin.Copyable, the default
implementation of copyElement does not copy dynamic properties. The default value of
NonCopyable is true for dynamic properties.
The default implementation of copyElement honors the value of a dynamic property NonCopyable
attribute. If you want to allow copying of a dynamic property, set its NonCopyable attribute to
false. Copying a dynamic property copies the property value and the values of the property
attributes.
For example, this copy operation copies the dynamic property, DynoProp, because its NonCopyable
attribute is set to false. The object obj must be an instance of a class that derives from both
dynamicprops and matlab.mixin.Copyable:
obj = MyDynamicClass;
p = addprop(obj,'DynoProp');
7-34
Implement Copy for Handle Classes
p.NonCopyable = false;
obj2 = copy(obj);
See Also
matlab.mixin.Copyable
Related Examples
• “Dynamic Properties — Adding Properties to an Instance” on page 8-47
7-35
8
In this section...
“What Are Properties” on page 8-2
“Types of Properties” on page 8-2
In some ways, properties are like fields of a struct object. However, storing data in an object
property provides more flexibility. Properties can:
• Define a constant value that you cannot change outside the class definition. See “Define Class
Properties with Constant Values” on page 15-2.
• Calculate its own value based on the current value of other data. See “Features of Dependent
Properties” on page 8-3.
• Execute a function to determine if an attempt to assign a value meets a certain criteria. See
“Property Get and Set Methods” on page 8-38.
• Trigger an event notification when any attempt is made to get or set its value. See “Property-Set
and Query Events” on page 11-14.
• Control access by code to the property values. See the SetAccess and GetAccess attributes
“Property Attributes” on page 8-8.
• Control whether its value is saved with the object in a MAT-file. See “Save and Load Objects” on
page 13-2.
For an example of a class that defines and uses a class, see “Creating a Simple Class” on page 2-2.
Types of Properties
There are two types of properties:
8-2
Ways to Use Properties
• Can use a set access method to control possible values when set
Dependent properties save memory because property values that depend on other values are
calculated only when needed.
• Compute the value of a property from other values (for example, you can compute area from
Width and Height properties).
• Provide a value in different formats depending on other values. For example, the size of a push
button in values determined by the current setting of its Units property.
• Provide a standard interface where a particular property is or is not used, depending on other
values. For example, different computer platforms can have different components on a toolbar).
For examples of classes that use dependent properties, see “Calculate Data on Demand” on page 3-17
and “A Class Hierarchy for Heterogeneous Arrays” on page 20-2.
See Also
Related Examples
• “Property Attributes” on page 8-8
• “Validate Property Values” on page 8-18
• “Property Get and Set Methods” on page 8-38
• “Static Properties” on page 5-38
8-3
8 Properties — Storing Class Data
Property Syntax
In this section...
“Property Definition Block” on page 8-4
“Property Validation Syntax” on page 8-5
“Property Access Syntax” on page 8-6
This topic describes how to define class properties in MATLAB using properties...end blocks, and
it introduces property validation syntax and concepts. It also covers the basics of getting and setting
property values from a class instance.
The properties and end keywords define one or more class properties that have the same attribute
settings. This is the general syntax for defining a property block:
properties (attributes)
propName1
...
propNameN
end
Note Properties cannot have the same name as the class or any of the other members defined by the
class.
For example, this properties block defines two properties with the SetAccess attribute set to
private. This attribute setting means that the property values can be set only by members of the
PrivateProps class.
classdef PrivateProps
properties (SetAccess = private)
Property1
Property2
end
end
You can also define multiple property blocks for properties with different attributes. In this example,
one properties block defines properties with private SetAccess, and the second block defines an
abstract property. Property blocks with different attributes can appear in any order in the class
definition.
classdef MultiplePropBlocks
properties (SetAccess = private)
Property1
Property2
end
properties (Abstract)
Property3
end
end
8-4
Property Syntax
For a full listing of property attributes, see “Property Attributes” on page 8-8.
properties (attributes)
propName1 (dimensions) class {validators} = defaultValue
...
end
This class defines one property. The properties block has no explicit attribute defined, which is
equivalent to defining a block of public properties. MyPublicData must also be a vector of positive
doubles, and it has a default value of [1 1 1].
classdef ValidationExample
properties
MyPublicData (1,:) double {mustBePositive} = [1 1 1]
end
end
Not all validation options must be used at once, and different properties in the same block can use
different combinations of validators. In this example, the RestrictedByClass property uses class
validation only, while RestrictedByFunction uses a validation function and assigns a default
value.
classdef DifferentValidation
properties
RestrictedByClass uint32
RestrictedByFunction {mustBeInteger} = 0
8-5
8 Properties — Storing Class Data
end
end
For more information, see “Property Class and Size Validation” on page 8-23 and “Property
Validation Functions” on page 8-29.
val = obj.PropertyName
Assign values to properties by putting the property reference on the left side of the equal sign.
obj.PropertyName = val
For example, instantiate the ValidationExample class and read the value of MyPublicData.
classdef ValidationExample
properties
MyPublicData (1,:) double {mustBePositive} = [1 1 1]
end
end
x = ValidationExample;
x.MyPublicData
ans =
1 1 1
Assign a new value to the property that satisfies the validators defined for it.
x.MyPublicData = [2 3 5 7];
You can optionally define get and set methods that MATLAB automatically calls when you use this
structure field syntax. For more information, see “Property Get and Set Methods” on page 8-38.
MATLAB can resolve a property name from a string or char variable using an expression of the
form:
object.(PropertyNameVar)
PropertyNameVar is a variable containing the name of a valid object property. Use this syntax when
passing property names as arguments. For example, the getPropValue function returns the value of
the KeyType property.
PropName = "KeyType";
function o = getPropValue(obj,PropName)
o = obj.(PropName);
end
8-6
Property Syntax
See Also
Related Examples
• “Property Attributes” on page 8-8
• “Validate Property Values” on page 8-18
• “Initialize Property Values” on page 8-13
8-7
8 Properties — Storing Class Data
Property Attributes
In this section...
“Purpose of Property Attributes” on page 8-8
“Specify Property Attributes” on page 8-8
“Table of Property Attributes” on page 8-8
“Property Access Lists” on page 8-12
You can use a simpler syntax for attributes whose values are true. The attribute name by itself
implies true, and adding the not operator (~) to the name implies false. For example, this block
defines abstract properties.
properties (Abstract)
...
end
All properties support the attributes listed in this table. Attribute values apply to all properties
defined within the properties...end code block that specifies the nondefault values. Attributes
that you do not explicitly defined take their default values.
8-8
Property Attributes
Property Attributes
8-9
8 Properties — Storing Class Data
8-10
Property Attributes
8-11
8 Properties — Storing Class Data
For Prop1:
For Prop2:
See Also
Related Examples
• “Property Syntax” on page 8-4
• “Initialize Property Values” on page 8-13
8-12
Initialize Property Values
• Define properties with default values — MATLAB assigns the same initial value to the property
of every instance.
• Set property values in the constructor — The constructor evaluates the assignment statement
for each instance, which enables instances to have unique initial property values.
classdef PropExample
properties
Prop1
Prop2 = "some text"
Prop3 = sin(pi/12)
Prop4 = datetime.empty
Prop5 (1,1) double {mustBePositive} = 1
end
end
• Prop1 — The property definition does not specify a default value, so MATLAB initializes the
property value to an empty double ([]).
• Prop2 — The default value is the string scalar "some text".
• Prop3 — The default value is the value of sin(pi/12). Reading this property returns the
evaluated expression (0.2588), not the expression itself.
For more information on the evaluation of expressions that you assign as default values, see
“Evaluation of Expressions in Class Definitions” on page 6-9 and “Properties Containing Objects”
on page 8-45.
• Prop4 — The default value is an empty datetime object.
• Prop5 — The default value is 1, and the property value in general is restricted to scalar, positive
doubles. When a property definition specifies any size, class, or validation function restrictions on
the property value, then the default value must satisfy those conditions. For example, a default
value of 0 would cause an error during instantiation because it does not satisfy mustBePositive.
For information on property restrictions based on size, class, and validation functions, see
“Validate Property Values” on page 8-18.
Note MATLAB evaluates a default expression when the property value is first needed (for example,
when the class is first instantiated). The same default value is then used for all instances of a class.
8-13
8 Properties — Storing Class Data
MATLAB does not reevaluate the default expression unless the class definition is cleared from
memory.
When you use a handle class constructor to create a default property value, MATLAB calls the
constructor only when the class is first used, and then uses the same object handle as the default for
the property in all instances. Because all of the object handles reference the same object, any
changes you make to the handle object in one instance are reflected in the handle object in all
instances. To initialize a property value with a new instance of a handle object each time you
instantiate your class, assign the property value in the constructor.
When you assign a value to a property in the class constructor, MATLAB evaluates the assignment
statement for each object you create. Assign property values in the constructor if you want each
object to contain a unique value for that property.
For example, ContainsHandle assigns a unique handle object of class MyHandleClass to Prop1
for each instance. ContainsHandle does this by calling the MyHandleClass constructor from its
own constructor.
classdef ContainsHandle
properties
Prop1
end
methods
function obj = ContainsHandle(keySet,valueSet)
obj.Prop1 = MyHandleClass(keySet,valueSet);
end
end
end
For more information on constructor methods, see “Referencing the Object in a Constructor” on page
9-18.
MATLAB validates default property values before the assignment of values in the constructor. The
default value assigned in the properties block and any value set for the property in the class
constructor must satisfy the specified validation. For example, PropInit restricts Prop to a scalar
8-14
Initialize Property Values
positive double, but it does not assign a default value. By default, MATLAB assigns an initial value of
empty double.
classdef PropInit
properties
Prop (1,1) double {mustBePositive}
end
methods
function obj = PropInit(positiveInput)
obj.Prop = positiveInput;
end
end
end
Calling the class constructor with a valid value for Prop still causes an error because of the initial
empty double in Prop. An empty double does not satisfy the validation function mustBePositive.
obj = PropInit(2);
Error using implicit default value of property 'Prop' of class 'PropInit':
Value must be positive.
To avoid this problem, ensure that your properties have default values that satisfy your validation,
even when you intend to overwrite those values in the constructor.
See Also
Related Examples
• “Evaluation of Expressions in Class Definitions” on page 6-9
• “Ways to Use Properties” on page 8-2
• “Validate Property Values” on page 8-18
8-15
8 Properties — Storing Class Data
In this section...
“Set Access to Property Values” on page 8-16
“Define Immutable Property” on page 8-16
• SetAccess = public — Any code with access to an object can set public property values. There
are differences between the behavior of handle and value classes with respect to modifying object
properties.
• SetAccess = protected — Only code executing from within class methods or methods of
subclasses can set property values. You cannot change the value of an object property unless the
class or any of its subclasses defines a method to do so.
• SetAccess = private — Only the defining class can set property values. You can change the
value of an object property only if the class defines a method that sets the property.
• SetAccess = immutable — Property value is set during construction. You cannot change the
value of an immutable property after the object is created. Set the value of the property as a
default or in the class constructor. You cannot define a property set method (set.PropertyName)
for an immutable property.
classdef Immute
properties (SetAccess = immutable)
CurrentDate
end
methods
function obj = Immute
obj.CurrentDate = datetime("today");
end
end
end
a = Immute
a =
CurrentDate: 09-Jun-2022
8-16
Mutable and Immutable Properties
See Also
Related Examples
• “Property Attributes” on page 8-8
• “Object Modification” on page 5-35
8-17
8 Properties — Storing Class Data
MATLAB property validation enables you to place specific restrictions on property values. You can use
validation to constrain the class and size of property values. Also, you can use functions to establish
criteria that the property value must conform to. MATLAB defines a set of validation functions and
you can write your own validation functions.
For more information on property validation, see “Property Class and Size Validation” on page 8-23,
“Property Validation Functions” on page 8-29, and “Metadata Interface to Property Validation” on
page 8-36.
Validation Syntax
The highlighted area in the following code shows the syntax for property validation.
• Size — The length of each dimension, specified as a positive integer or a colon. A colon indicates
that any length is allowed in that dimension. The value assigned to the property must conform to
the specified size or be compatible with the specified size. For more information, see “Property
Size Validation” on page 8-23.
• Class — The name of a single MATLAB class. The value assigned to the property must be of the
specified class or convertible to the specified class. Use any MATLAB class or externally defined
class that is supported by MATLAB, except for Java and COM classes. For more information, see
“Property Class Validation” on page 8-24.
8-18
Validate Property Values
• Functions — A comma-separated list of validation function names. MATLAB passes the value
assigned to the property to each the validation functions after applying any possible class and size
conversions. Validator functions throw errors if the validation fails, but do not return values. For
more information, see “Property Validation Functions” on page 8-29.
For a list of MATLAB validation functions, see “Property Validation Functions” on page 8-29.
Use property validation for public properties to control the values user code assigns to the properties.
If you want to restrict property values to a fixed set of identifiers, create an enumeration class for
these identifiers and constrain the property to this class. For information on enumeration classes, see
“Define Enumeration Classes” on page 14-4.
MATLAB type conversion rules apply to property validation. For example, MATLAB can coerce from
one to another numeric type. Therefore, restricting a property value to a specific numeric type, such
as double does not prevent the assignment of other numeric types to the property.
To ensure that a property can be assigned only a specific type of value, restrict the property to a type
that supports only the desired type conversions or use a validation function to specify the exact class
allowed for the property instead of specifying the property type. MATLAB evaluates the type
specification before executing any validation functions. For more information, see “Order of
Validation” on page 8-20.
Ensure that any default value assigned to the property meets the restrictions imposed by the
specified validation. If you do not specify a default value, MATLAB creates a default value by
assigning an empty object of the specified class or by calling the default constructor if size restriction
does not allow the use of an empty default value. The default constructor must return an object of the
correct size.
• Location must be a 1-by-3 array of class double whose values are real, finite numbers.
• Label must be a string scalar that is either "High", "Medium", or "Low".
• State must be an enumeration member of the matlab.lang.OnOffSwitchState class (off or
on).
Validation at Instantiation
Creating an object of the ValidateProps class performs the validation on implicit and explicit
default values:
a = ValidateProps
8-19
8 Properties — Storing Class Data
a =
Location: [0 0 0]
Label: "Low"
State: off
• Initializes the Location property value to [0 0 0] to satisfy the size and class requirements.
• Sets the Label property to its default value, "Low". The default value must be a member of the
allowed set of values. The empty string default value would cause an error.
• Sets the State property to the off enumeration member defined by the
matlab.lang.OnOffSwitchState class.
For information on how MATLAB selects default values, see “Default Values Per Size and Class” on
page 8-28.
Order of Validation
When a value is assigned to the property, including default values that are specified in the class
definition, MATLAB performs validation in this order:
• Class validation — This validation can cause conversion to a different class, such as conversion of
a char to string. Assignment to properties follows MATLAB conversion rules for arrays.
• Size validation — This validation can cause size conversion, such as scalar expansion or
conversion of a column vector to a row vector. Assignment to a property that specifies a size
validation behaves the same as assignment to any MATLAB array. For information on indexed
assignment, see “Array Indexing”.
• Validator functions — MATLAB passes the result of the class and size validation to each validation
function, in left to right order. An error can occur before all validation functions have been called,
which ends the validation process.
• Set method — MATLAB performs property validation before calling a property set method, if one
is defined for that property. Assignment to the property within a property set or get method does
not apply the validation again. Often, you can replace property set methods using property
validation.
The ValueProp class uses size, class, and function validation to ensure that an assignment to the
Value property is a double scalar that is not negative.
classdef ValueProp
properties
Value(1,1) double {mustBeNonnegative} = 0
end
end
This statement attempts to assign a cell array to the property. This assignment violates the class
validation.
a.Value = {10,20};
8-20
Validate Property Values
This statement attempts to assign a 1-by-2 double array to the property. This assignment violates the
size validation.
a.Value = [10 20];
This statement attempts to assign a scalar double to the property. This assignment fails the function
validation, which requires a nonnegative number.
a.Value = -10;
If a class definition changes the property validation such that the loaded property value is no longer
valid, MATLAB substitutes the currently defined default value for that property. However, the load
function suppresses the validation errors that occur before assigning the default value from the
current class definition. Therefore, validation errors are silently ignored during load operations.
To illustrate this behavior, this example creates, saves, and loads an object of the MonthTemp class.
This class restricts the AveTemp property to a cell array.
classdef MonthTemp
properties
AveTemp cell
end
end
8-21
8 Properties — Storing Class Data
a = MonthTemp;
a.AveTemp = {'May',70};
save TemperatureFile a
Edit the property definition to change the validation class for the AveTemp property from cell array to
containers.Map.
classdef MonthTemp
properties
AveTemp containers.Map
end
end
Load the saved object with the new class definition on the MATLAB path. MATLAB cannot assign the
saved value to the AveTemp property because the cell array, {'May',70}, is not compatible with the
current requirement that the property value be a containers.Map object. MATLAB cannot convert
a cell array to a containers.Map.
To address the incompatibility, MATLAB sets the AveTemp property of the loaded object to the
current default value, which is an empty containers.Map object.
load TemperatureFile a
a.AveTemp
ans =
Count: 0
KeyType: char
ValueType: any
The loaded object has a different value assigned to the AveTemp property because the saved value is
now invalid. However, the load process suppresses the validation error.
To prevent loss of data when changing class definitions and reloading objects, implement a loadobj
method or class converter method that enables the saved values to satisfy the current property
validation.
For more information on saving and loading objects, see “Save and Load Process for Objects” on page
13-2.
See Also
Related Examples
• “Property Class and Size Validation” on page 8-23
• “Property Validation Functions” on page 8-29
8-22
Property Class and Size Validation
For more information, see “Order of Validation” on page 8-20 and “Property Validation Functions” on
page 8-29.
classdef MyClass
properties
Prop(dim1,dim2,...) = defaultValue
end
end
This class defines the size of the Location property as 1-by-3. Any value assigned to this property
must conform to that size or must be convertible to that size.
classdef ValidateProps
properties
Location(1,3)
end
end
The implicit default value assigned by MATLAB, [0 0 0], conforms to the specified size:
a = ValidateProps
a =
Location: [0 0 0]
MATLAB applies scalar expansion when you assign a scalar the Location property.
a = ValidateProps;
a.Location = 1
8-23
8 Properties — Storing Class Data
a =
Location: [1 1 1]
col =
1
1
1
a.Location = col
a =
Location: [1 1 1]
A colon in the size specification indicates that the corresponding dimension can have any length. For
example, you can assign a row vector with any number of columns to the Data property in this class.
classdef ValidateProps
properties
Data(1,:)
end
end
a = ValidateProps;
a.Data = [1:10]
a =
Data: [1 2 3 4 5 6 7 8 9 10]
Assignment to a property that defines size validation follows the same rules as the equivalent indexed
array assignment. For information on indexing behavior of multidimensional arrays, see “Compatible
Array Sizes for Basic Operations”.
You can specify only one class per property. Use validation functions like mustBeNumeric or
mustBeInteger to restrict properties to a broader category of classes. For more information on
validation functions, see “Property Validation Functions” on page 8-29.
8-24
Property Class and Size Validation
You can use any MATLAB class or externally defined class that is supported by MATLAB, except Java
and COM classes.
Place the name of the class in the property definition block following the property name and optional
size specification.
classdef MyClass
properties
Prop ClassName = defaultValue
end
end
If you do not specify a default value, MATLAB assigns an empty object of the specified class to the
property. If you define a size and a class, MATLAB attempts to create a default value for the property
that satisfies both the size and class requirement.
MATLAB creates the default value by calling the class constructor with no arguments. The class must
have a constructor that returns an object of the specified size when called with no input arguments or
you must specify a default value for the property that satisfies the property size restriction. For more
information, see “Default Values Per Size and Class” on page 8-28.
classdef PropsWithClass
properties
Number double
Today string = datetime("today")
end
end
p = PropsWithClass
p =
Number: []
Today: "09-Jun-2022"
MATLAB performs conversions from any compatible class to the property class. For example, assign a
uint8 value to the Number property.
p.Number = uint8(5);
disp(class(p.Number))
ans =
double
8-25
8 Properties — Storing Class Data
Because the uint8 value can be converted to double, you can assign that uint8 value to the
Number property.
Assigning an incompatible value to a property that uses class validation causes an error.
p.Number = datetime("today");
You can define a class to control the values assigned to a property. Enumeration classes enable users
to set property values to character vectors or string scalars with inexact name matching.
For example, suppose that there is a class that represents a three-speed mechanical pump. You can
define an enumeration class to represent the three flow rates.
classdef FlowRate < int32
enumeration
Low (10)
Medium (50)
High (100)
end
end
The Pump class has a method to return the current flow rate in gallons per minute. Define the Speed
property as a FlowRate class.
classdef Pump
properties
Speed FlowRate
end
methods
function getGPM(p)
if isempty(p.Speed)
gpm = 0;
else
gpm = int32(p.Speed);
end
fprintf('Flow rate is: %i GPM\n',gpm);
end
end
end
p =
Speed: Medium
8-26
Property Class and Size Validation
For information about enumeration classes, see “Define Enumeration Classes” on page 14-4.
MATLAB supports several integer classes (see “Integers”). However, restricting a property to an
integer class can result in integer overflow. The resulting value can saturate at the maximum or
minimum value in the integer’s range.
When integer overflow occurs, the value that is assigned to a property can be a value that is different
from the value from the right side of the assignment statement.
For example, suppose that you want to restrict a property value to a scalar uint8.
classdef IntProperty
properties
Value(1,1) uint8
end
end
Assigning a numeric value to the Value property effectively casts the numeric value to uint8, but
does not result in an error for out-of-range values.
a = IntProperty;
a.Value = -10;
disp(a.Value)
Assignment to the Value property is equivalent to indexed assignment of an array. If the assigned
value is out of the range of values that uint8 can represent, MATLAB sets the value to the closest
value that it can represent using uint8.
a = uint8.empty;
a(1) = -10
a =
uint8
To avoid the potential for integer overflow, use a combination of validation functions that restrict the
value to the intended range instead of an integer class.
classdef IntProperty
properties
Value(1,1) {mustBeInteger, mustBeNonnegative,...
mustBeLessThan(Value,256)}
end
end
Because there is no conversion of the assigned value by the uint8 class, the validators catch out of
range values and throw an appropriate error.
a = IntProperty;
a.Value = -10;
8-27
8 Properties — Storing Class Data
MATLAB defines a default value implicitly if you do not specify a default value in the class definition.
This table shows how size and class determine the implicit default value of MATLAB classes.
To determine the implicit default value for nonzero and explicit size specifications, MATLAB calls the
default class constructor and builds an array of the specified size using the instance returned by the
constructor call. If the class does not support a default constructor (that is, a constructor called with
no arguments), then MATLAB throws an error when instantiating the class containing the validation.
If the specified size has any zero or unrestricted (:) dimensions, MATLAB creates a default value that
is an empty array with the unrestricted dimension set to zero.
See Also
Related Examples
• “Validate Property Values” on page 8-18
• “Property Validation Functions” on page 8-29
• “Enumerations for Property Values” on page 14-14
8-28
Property Validation Functions
8-29
8 Properties — Storing Class Data
Data Types
Size
8-30
Property Validation Functions
Text
During the validation process, MATLAB passes the value to each validation function listed in the class
definition. MATLAB calls each function from left to right and throws the first error encountered. The
value passed to the validation functions is the result of any conversion applied by the class and size
specifications. For more information on class and size validation, see “Property Class and Size
Validation” on page 8-23.
For a list of MATLAB validation functions, see “MATLAB Validation Functions” on page 8-29.
Specify validation functions as a comma-separated list of function names or function calls with
arguments, enclosed in braces.
classdef MyClass
properties
8-31
8 Properties — Storing Class Data
MATLAB passes the potential property value to the validation function implicitly. However, if the
validation function requires input arguments in addition to the potential property value, then you
must include both the property and the additional arguments. Additional arguments must be literal
values and cannot reference variables. Literal values are nonsymbolic representations, such as
numbers and text.
For example, consider the function mustBeGreaterThan. It requires a limiting value as an input
parameter. This validation function requires that a property value must be greater than this limiting
value.
Pass the property as the first argument. Use the property name, but do not enclose the name in
quotation marks. This property definition restricts Prop to values greater than 10.
properties
Prop {mustBeGreaterThan(Prop,10)}
end
a = ValidatorFunction
a =
Data: []
Interp: 'linear'
a.Data = 'cubic'
Because the Data property validation does not include a numeric class, there is no conversion of the
char vector to a numeric value. If you change the validation of the Data property to specify the class
as double, MATLAB converts the char vector to a double array.
8-32
Property Validation Functions
properties
Data double {mustBeNumeric, mustBeFinite}
end
The assignment to the char vector does not produce an error because MATLAB converts the char
vector to class double.
a.Data = 'cubic'
a =
a = ValidatorFunction;
a.Interp = 'cu'
For example, suppose that you define the InterpMethod enumeration class for the Interp property
validation.
classdef InterpMethod
enumeration
linear
cubic
spline
end
end
classdef ValidatorFunction
properties
Data {mustBeNumeric, mustBeFinite}
Interp InterpMethod
end
end
8-33
8 Properties — Storing Class Data
a = ValidatorFunction;
a.Interp = 'cu'
a =
Data: []
Interp: cubic
Creating your own validation function is useful when you want to provide specific validation that is
not available using the MATLAB validation functions. You can create local functions within the class
file or place the function on the MATLAB path to be available for use in any class.
For example, the ImgData class uses a local function to define a validator that restricts the Data
property to only uint8 or uint16 values, excluding subclasses and not allowing conversion from
other numeric classes. The predefined validation function mustBeInRange restricts the range of
allowed values.
classdef ImgData
properties
Data {mustBeImData(Data),mustBeInRange(Data,0,255)} = uint8(0)
end
end
function mustBeImData(a)
if ~(isa(a,'uint8') || isa(a,'uint16'))
error("Value of Data property must be uint8 or uint16 data.")
end
end
When you create an instance of the ImgData class, MATLAB validates that the default value is a
uint8 or uint16 value, in the range 0...255, and not empty. Note that the default value must
satisfy the validation requirements like any other value assigned to the property.
a = ImgData
a =
Data: 0
Property assignment invokes the validators in left-to-right order. Assigning a char vector to the Data
property causes an error thrown by mustBeImData.
a.Data = 'red';
Error setting property 'Data' of class 'ImgData'.
Value of Data property must be uint8 or uint16 data.
Assigning a numeric value that is out of range causes an error thrown by mustBeInRange.
8-34
Property Validation Functions
a.Data = uint16(312);
Error setting property 'Data' of class 'ImgData'. Value must be greater
than or equal to 0, and less than or equal to 255.
See Also
Related Examples
• “Validate Property Values” on page 8-18
• “Property Class and Size Validation” on page 8-23
8-35
8 Properties — Storing Class Data
You can determine what validation applies to a property by accessing the validation metadata.
Instances of the meta.Validation class provide the following information about property
validation.
For example, the ValidationExample class defines a property that must be an array of doubles that
is 1-by-any number of elements and must be a real number that is greater than 10.
classdef ValidationExample
properties
Prop (1,:) double {mustBeReal, mustBeGreaterThan(Prop, 10)} = 200;
end
end
Access the meta.Validation object from the property's meta.property object. Get the validation
information from the meta.Validation object properties. Collection this information into a cell
array.
mc = ?ValidationExample;
mp = findobj(mc.PropertyList,'Name','Prop');
sz = mp.Validation.Size;
len = length(sz);
dim = cell(1:len);
for k = 1:len
switch class(sz(k))
case 'meta.FixedDimension'
dim{k} = sz(k).Length;
case 'meta.UnrestrictedDimension'
dim{k} = ':';
end
end
dim{end+1} = mp.Validation.Class.Name;
dim{end+1} = mp.Validation.ValidatorFunctions;
See Also
meta.property | meta.Validation
8-36
Metadata Interface to Property Validation
Related Examples
• “Validate Property Values” on page 8-18
• “Property Class and Size Validation” on page 8-23
• “Property Validation Functions” on page 8-29
8-37
8 Properties — Storing Class Data
You can define property get and set methods that MATLAB calls automatically whenever the
associated property is accessed. To associate a get or set method with a given property, name the get
and set methods using the forms get.PropertyName and set.PropertyName, respectively.
Get and set methods can perform extra steps beyond just accessing the property. Use get methods to:
• Design property validation that is more complex than what the built-in validation techniques
support.
• Issue custom error messages.
• Perform actions that are a direct result of a property value change, such as establishing or
updating connections with hardware devices or opening files, ensuring access to resources.
Get and set methods do add overhead to your classes. Avoid complex and computation-heavy
operations in the get and set methods of frequently accessed properties.
Note You cannot call the get and set methods described in this topic directly. MATLAB
automatically calls these methods when you access property values. For information on implementing
user-callable get and set methods, see “Implement Set/Get Interface for Properties” on page 7-22.
methods
function value = get.PropertyName(obj)
...
end
end
For example, the triangleArea class defines a get method for the Area property. Area is defined as
a dependent property, which means that it does not store values. The get method for Area calculates
the value on demand. (For more information on dependent properties, see “Get and Set Methods for
Dependent Properties” on page 8-42.)
classdef triangleArea
properties
8-38
Property Get and Set Methods
Base = 1
Height = 1
end
properties (Dependent)
Area
end
methods
function a = get.Area(obj)
disp("Executing get.Area method.")
a = 0.5*obj.Base*obj.Height;
end
end
end
a = triangleArea
a =
Base: 1
Height: 1
Area: 0.5000
When displaying an object, MATLAB calls any defined get methods for the properties it displays. In
this case, it calls get.Area and calculates the value of Area based on the default values for Base
and Height. If a get method errors, MATLAB suppresses the error and omits that property from the
display.
Change the values of Base and Height and access Area again.
a.Base = 3;
a.Height = 4;
a.Area
ans =
8-39
8 Properties — Storing Class Data
methods
function obj = set.PropertyName(obj,value)
...
end
end
• Handle class set methods do not need to return the modified object.
methods
function set.PropertyName(obj,value)
...
end
end
For example, symPosDef uses a set method for property validation. When the inputMatrix
property is set to a new value, the set method calls the chol function to determine if the input matrix
is symmetric positive definite. If it is, the method sets inputMatrix to that value. If not, the method
returns a custom error message.
classdef symPosDef
properties
inputMatrix = [1 0; 0 1]
end
methods
function obj = set.inputMatrix(obj,val)
try chol(val)
obj.inputMatrix = val;
catch ME
error("inputMatrix must be symmetric positive definite.")
end
end
end
end
Create an instance of symPosDef and try to set inputMatrix to a value that is not a symmetric
positive definite matrix.
s = symPosDef;
s.inputMatrix = [1 2; 1 1]
8-40
Property Get and Set Methods
• When a property is defined with the AbortSet attribute equal to true, the set method of the
property is not called when assigning a value that is the same as the current value. However, if the
property has a get method, that method is called so that the values can be compared. See
“Assignment When Property Value Is Unchanged” on page 11-35 for more information on this
attribute.
See Also
Related Examples
• “Get and Set Methods for Dependent Properties” on page 8-42
• “Listen for Changes to Property Values” on page 11-32
8-41
8 Properties — Storing Class Data
Dependent properties do not store data. The value of a dependent property depends on other values,
such as the values of nondependent properties. Define a dependent property using this syntax:
properties (Dependent)
PropertyName
end
Because dependent properties do not store data, you must define get methods (get.PropertyName)
to determine the value for the properties when the properties are queried.
Dependent properties can also have set methods (set.PropertyName), but these methods cannot
actually set the value of the dependent property. However, a set method can contain other code. For
example, it can set values of properties related to the dependent property.
For an introduction to defining get and set methods, see “Property Get and Set Methods” on page 8-
38.
8-42
Get and Set Methods for Dependent Properties
a = Account;
a.USDollarAmount = 100;
a.Currency = "JPY";
You cannot explicitly call get methods. When you access Balance, MATLAB calls the get method to
return the initial amount converted to yen.
a.Balance
ans =
1.4493e+04
MATLAB also calls get methods when it displays the object. When you set Currency to euros without
ending the statement with a semicolon, MATLAB calls the Balance get method to display the
updated value.
a.Currency = "EUR"
a =
Currency: "EUR"
USDollarAmount: 100
Balance: 102.0400
For example, propertyChange is a value class that initially defined a property named
OldPropName. You can use a set method to change the property name from the perspective of a class
user:
classdef propertyChange
properties
NewPropName
end
properties (Dependent,Hidden)
OldPropName
end
methods
function obj = set.OldPropName(obj,val)
obj.NewPropName = val;
end
function value = get.OldPropName(obj)
value = obj.NewPropName;
end
8-43
8 Properties — Storing Class Data
end
end
Code that accesses OldPropName continues to work as expected, and making OldPropName hidden
helps prevent new users from seeing the old property name.
For example, create an instance of propertyChange. Set the property value using the old property
name and then display the object. MATLAB sets the value to the property with the new name and
displays it.
a = propertyChange;
a.OldPropName = "hello"
p =
NewPropName: "hello"
See Also
Related Examples
• “Property Attributes” on page 8-8
• “Calculate Data on Demand” on page 3-17
8-44
Properties Containing Objects
Note Evaluation of property default values occurs only when the value is first needed, and only once
when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you
create an instance of the class.
For more information on the evaluation of expressions that you assign as property default values, see
“When MATLAB Evaluates Expressions” on page 6-11.
• Handle object – you can set properties on handle objects contained in read-only properties
• Value object – you cannot set properties on value object contained in read-only properties.
Assignment Behavior
These classes illustrate the assignment behavior:
• ReadOnlyProps – class with two read-only properties. The class constructor assigns a handle
object of type HanClass to the PropHandle property and a value object of type ValClass to the
PropValue property.
• HanClass – handle class with public property
• ValClass – value class with public property
classdef ReadOnlyProps
properties(SetAccess = private)
PropHandle
PropValue
end
methods
function obj = ReadOnlyProps
obj.PropHandle = HanClass;
8-45
8 Properties — Storing Class Data
obj.PropValue = ValClass;
end
end
end
classdef ValClass
properties
Vprop
end
end
a = ReadOnlyProps
a =
Use the private PropHandle property to set the property of the HanClass object it contains:
class(a.PropHandle.Hprop)
ans =
double
a.PropHandle.Hprop = 7;
Attempting to make an assignment to the value class object property is not allowed:
a.PropValue.Vprop = 11;
See Also
More About
• “Mutable and Immutable Properties” on page 8-16
8-46
Dynamic Properties — Adding Properties to an Instance
It is possible for more than one program to define dynamic properties on the same object. In these
cases, avoid name conflicts. Dynamic property names must be valid MATLAB identifiers (see
“Variable Names”) and cannot be the same name as a method of the class.
• Set and query the values of dynamic properties using dot notation. (See “Assign Data to the
Dynamic Property” on page 8-48.)
• MATLAB saves and loads dynamic properties when you save and load the objects to which they
are attached. (See “Dynamic Properties and ConstructOnLoad” on page 8-57.)
• Define attributes for dynamic property. (See “Set Dynamic Property Attributes” on page 8-48.)
• By default, dynamic properties have their NonCopyable attribute set to true. If you copy an
object containing a dynamic property, the dynamic property is not copied. (See “Objects with
Dynamic Properties” on page 7-34.)
• Add property set and get access methods. (See “Set and Get Methods for Dynamic Properties” on
page 8-51.)
• Listen for dynamic property events. (See “Dynamic Property Events” on page 8-53.)
• Access dynamic property values from object arrays, with restricted syntax. (See “Accessing
Properties and Methods in Object Arrays” on page 10-6.)
• The isequal function always returns false when comparing objects that have dynamic
properties, even if the properties have the same name and value. To compare objects that contain
dynamic properties, overload isequal for your class.
where:
8-47
8 Properties — Storing Class Data
H is an array of handles
PropertyName is the name of the dynamic property you are adding to each object
Use only valid names when naming dynamic properties (see “Variable Names”). In addition, do not
use names that:
To set property attributes, use the meta.DynamicProperty object associated with the dynamic
property. For example, if P is the object returned by addprop, this statement sets the property’s
Hidden attribute to true:
P.Hidden = true;
The property attributes Constant and Abstract have no meaning for dynamic properties. Setting
the value of these attributes to true has no effect.
Suppose, you are using a predefined set of user interface widget classes (buttons, sliders, check
boxes, etc.). You want to store the location of each instance of the widget class. Assume that the
widget classes are not designed to store location data for your particular layout scheme. You want to
avoid creating a map or hash table to maintain this information separately.
Assuming the button class is a subclass of dynamicprops, add a dynamic property to store your
layout data. Here is a simple class to create a uicontrol button:
classdef button < dynamicprops
properties
UiHandle
end
methods
function obj = button(pos)
if nargin > 0
if length(pos) == 4
obj.UiHandle = uicontrol('Position',pos,...
'Style','pushbutton');
else
error('Improper position')
end
end
8-48
Dynamic Properties — Adding Properties to an Instance
end
end
end
Create an instance of the button class, add a dynamic property, and set its value:
b1 = button([20 40 80 20]);
b1.addprop('myCoord');
b1.myCoord = [2,3];
Access the dynamic property just like any other property, but only on the object on which you defined
it:
b1.myCoord
ans =
2 3
Using nonpublic Access with dynamic properties is not recommended because these properties
belong to specific instances that are often created outside of class methods. The Access attribute of a
dynamic property applies to the class of the instance that contains the dynamic property. The
dynamic property Access attribute does not necessarily apply to the class whose method adds the
dynamic property.
For example, if a base class method adds a dynamic property with private access to an instance, the
private access applies only to the class of the instance.
For more information on dynamic property attributes, see meta.DynamicProperty. Use the handle
findprop method to get the meta.DynamicProperty object.
• Get the names of the object's properties using the properties function.
• Get the metadata object for each property using findprop.
• Use the isa function to determine if the metadata object is a meta.DynamicProperty object. If
so, then the property is a dynamic property.
The getDynamicPropNames function shows how to display the names of any dynamic properties
defined for the input obj.
function getDynamicPropNames(obj)
% Find dynamic properties
allprops = properties(obj);
for i=1:numel(allprops)
m = findprop(obj,allprops{i});
if isa(m,'meta.DynamicProperty')
disp(m.Name)
end
end
end
8-49
8 Properties — Storing Class Data
See Also
Related Examples
• “Set and Get Methods for Dynamic Properties” on page 8-51
• “Dynamic Property Events” on page 8-53
• “Dynamic Properties and ConstructOnLoad” on page 8-57
8-50
Set and Get Methods for Dynamic Properties
• Define a function that implements the operations you want to perform before the property set or
get occurs. These methods must have the following signatures: mySet(obj,val) or val =
myGet(obj)
• Obtain the dynamic property's corresponding meta.DynamicProperty object.
• Assign a function handle referencing your set or get property function to the
meta.DynamicProperty object's GetMethod or SetMethod property. This function does not
need to be a method of the class. You cannot use a naming scheme like set.PropertyName.
Instead, use any other valid function name.
Suppose that you want to create a property set function for the myCoord dynamic property of the
button class created in “Define Dynamic Properties” on page 8-47.
function set_myCoord(obj,val)
if ~(length(val) == 2)
error('myCoords require two values')
end
obj.myCoord = val;
end
Because button is a handle class, the property set function does not need to return the object as an
output argument.
To get the meta.DynamicProperty object, use the handle class findprop method:
mb1 = b1.findprop('myCoord');
mb1.SetMethod = @set_myCoord;
MATLAB calls the property set function whenever you set this property:
You can set and get the property values only from within your property access methods. You cannot
call another function from the set or get method, and then attempt to access the property value from
that function.
8-51
8 Properties — Storing Class Data
See Also
Related Examples
• “Dynamic Properties — Adding Properties to an Instance” on page 8-47
8-52
Dynamic Property Events
In this section...
“Dynamic Properties and Ordinary Property Events” on page 8-53
“Dynamic-Property Events” on page 8-53
“Listen for a Specific Property Name” on page 8-54
“PropertyAdded Event Callback Execution” on page 8-55
“PropertyRemoved Event Callback Execution” on page 8-55
“How to Find meta.DynamicProperty Objects” on page 8-55
If you delete a dynamic property, and then create another dynamic property with the same name, the
listeners do not respond to events generated by the new property. A listener defined for a dynamic
property that has been deleted does not cause an error, but the listener callback is never executed.
“Property-Set and Query Events” on page 11-14 provides more information on how to define
listeners for these events.
Dynamic-Property Events
To respond to the addition and removal of dynamic properties, attach listeners to objects containing
the dynamic properties. The dynamicprops class defines events for this purpose:
• PropertyAdded — Triggered when you add a dynamic property to an object derived from the
dynamicprops class.
• PropertyRemoved — Triggered when you delete the object or the meta.DynamicProperty
object associated with a dynamic property.
• ObjectBeingDestroyed — Triggered when the object is destroyed. This event is inherited from
the handle class.
These events have public listen access (ListenAccess attribute) and private notify access
(NotifyAccess attribute).
8-53
8 Properties — Storing Class Data
• Set the value of a hidden property to true when a property named SpecialProp is added.
• Set the value of the hidden property to false when SpecialProp is removed.
Use the event.DynamicPropertyEvent event data to determine the name of the property and
whether it is added or deleted.
The DynamTest class derives from dynamicprops. It defines a hidden property, HiddenProp.
Define a callback function that uses the EventName property of the event data to determine if a
property is added or removed. Obtain the name of the property from the PropertyName property of
the event data. If a dynamic property is named SpecialProp, change the value of the hidden
property.
function DyPropEvtCb(src,evt)
switch evt.EventName
case 'PropertyAdded'
switch evt.PropertyName
case 'SpecialProp'
% Take action based on the addition of this property
%...
%...
src.HiddenProp = true;
disp('SpecialProp added')
otherwise
% Other property added
% ...
disp([evt.PropertyName,' added'])
end
case 'PropertyRemoved'
switch evt.PropertyName
case 'SpecialProp'
% Take action based on the removal of this property
%...
%...
src.HiddenProp = false;
disp('SpecialProp removed')
otherwise
% Other property removed
% ...
disp([evt.PropertyName,' removed'])
end
end
end
8-54
Dynamic Property Events
dt = DynamTest;
lad = addlistener(dt,'PropertyAdded',@DyPropEvtCb);
lrm = addlistener(dt,'PropertyRemoved',@DyPropEvtCb);
ad = addprop(dt,'SpecialProp');
The addition of the dynamic property causes the listener to execute its callback function,
DyPropEvtCb. The callback function assigns a value of true to the HiddenProp property.
dt.HiddenProp
ans =
Delete the meta.DynamicProperty object returned when adding the dynamic property
SpecialProp.
delete(ad)
SpecialProp removed
dt.HiddenProp
ans =
ad = findprop(dt,'SpecialProp');
8-55
8 Properties — Storing Class Data
See Also
Related Examples
• “Dynamic Properties — Adding Properties to an Instance” on page 8-47
8-56
Dynamic Properties and ConstructOnLoad
If you create dynamic properties from the class constructor, you can cause a conflict if you also set
the class ConstructOnLoad attribute to true. Here is the sequence:
• A saved object saves the names and values of properties, including dynamic properties
• When loaded, a new object is created and all properties are restored to the values at the time the
object was saved
• Then, the ConstructOnLoad attribute causes a call to the class constructor, which would create
another dynamic property with the same name as the loaded property. See “Save and Load
Objects” on page 13-2 for more on the load sequence.
• MATLAB prevents a conflict by loading the saved dynamic property, and does not execute
addprop when calling the constructor.
If you use ConstructOnLoad, add dynamic properties from the class constructor, and want the
constructor to call addprop at load time, then set the dynamic property Transient attribute to
true. This setting prevents the property from being saved. For example:
See Also
Related Examples
• “Dynamic Properties — Adding Properties to an Instance” on page 8-47
8-57
9
In this section...
“Class Methods” on page 9-2
“Examples and Syntax” on page 9-2
“Kinds of Methods” on page 9-2
“Method Naming” on page 9-3
Class Methods
Methods are functions that implement the operations performed on objects of a class. Methods, along
with other class members support the concept of encapsulation—class instances contain data in
properties and class methods operate on that data. This design allows the internal workings of
classes to be hidden from code outside of the class, and thereby enabling the class implementation to
change without affecting code that is external to the class.
Methods have access to private members of their class including other methods and properties. This
encapsulation enables you to hide data and create special interfaces that must be used to access the
data stored in objects.
For sample code and syntax, see “Method Syntax” on page 5-7.
For a discussion of how to create classes that modify standard MATLAB behavior, see “Methods That
Modify Default Behavior” on page 17-2.
For information on the use of @ and path directors and packages to organize your class files, see
“Class File Organization”.
For the syntax to use when defining classes in more than one file, see “Methods in Separate Files” on
page 9-8.
Kinds of Methods
There are specialized kinds of methods that perform certain functions or behave in particular ways:
• Ordinary methods are functions that act on one or more objects and return some new object or
some computed value. These methods are like ordinary MATLAB functions that cannot modify
input arguments. Ordinary methods enable classes to implement arithmetic operators and
computational functions. These methods require an object of the class on which to operate. See
“Ordinary Methods” on page 9-6.
• Constructor methods are specialized methods that create objects of the class. A constructor
method must have the same name as the class and typically initializes property values with data
obtained from input arguments. The class constructor method must declare at least one output
argument, which is the object being constructed. The first output is always the object being
constructed. See “Class Constructor Methods” on page 9-15.
9-2
Methods in Class Design
• Destructor methods are called automatically when the object is destroyed, for example if you call
delete(object) or there are no longer any references to the object. See “Handle Class
Destructor” on page 7-13.
• Property access methods enable a class to define code to execute whenever a property value is
queried or set. See “Property Get and Set Methods” on page 8-38.
• Static methods are functions that are associated with a class, but do not necessarily operate on
class objects. These methods do not require an instance of the class to be referenced during
invocation of the method, but typically perform operations in a way specific to the class. See
“Static Methods” on page 9-23.
• Conversion methods are overloaded constructor methods from other classes that enable your class
to convert its own objects to the class of the overloaded constructor. For example, if your class
implements a double method, then this method is called instead of the double class constructor
to convert your class object to a MATLAB double object. See “Object Converters” on page 17-5
for more information.
• Abstract methods define a class that cannot be instantiated itself, but serves as a way to define a
common interface used by numerous subclasses. Classes that contain abstract methods are often
referred to as interfaces. See “Abstract Classes and Class Members” on page 12-68 for more
information and examples.
Method Naming
The name of a function that implements a method can contain dots (for example,
set.PropertyName) only if the method is one of the following:
• Property set/get access method (see “Property Get and Set Methods” on page 8-38)
• Conversion method that converts to a package-qualified class, which requires the use of the
package name (see “Packages Create Namespaces” on page 6-21)
You cannot define property access or conversion methods as local functions, nested functions, or
separately in their own files. Class constructors and package-scoped functions must use the
unqualified name in the function definition; do not include the package name in the function
definition statement.
See Also
Related Examples
• “Method Attributes” on page 9-4
• “Rules for Naming to Avoid Conflicts” on page 9-27
9-3
9 Methods — Defining Class Operations
Method Attributes
In this section...
“Purpose of Method Attributes” on page 9-4
“Specifying Method Attributes” on page 9-4
“Table of Method Attributes” on page 9-4
Attribute values apply to all methods defined within the methods...end code block that specifies
the nondefault values.
9-4
Method Attributes
Method Attributes
See Also
metaclass | meta.method
More About
• “Methods”
9-5
9 Methods — Defining Class Operations
Ordinary Methods
In this section...
“Ordinary Methods Operate on Objects” on page 9-6
“Methods Inside classdef Block” on page 9-6
“Method Files” on page 9-7
Method attributes apply only to that particular methods block, which is terminated by the end
statement.
Note Nonstatic methods must include an explicit object variable as a function argument. The
MATLAB language does not support an implicit reference in the method function definition.
Example of a Method
The addData method adds a value to the Data property of MyData objects. The mustBeNumeric
function restricts the value of the Data property to numeric values. The property has a default value
of 0.
The addData method returns the modified object, which you can reassign to the same variable.
classdef MyData
properties
Data {mustBeNumeric} = 0
end
methods
function obj = addData(obj,val)
if isnumeric(val)
9-6
Ordinary Methods
a = MyData;
a = addData(a,75)
a =
Data: 75
Method Files
You can define methods:
For more information on class folders, see “Folders Containing Class Definitions” on page 6-14.
See Also
More About
• “Methods in Separate Files” on page 9-8
• “Method Invocation” on page 9-11
• “Operator Overloading” on page 17-19
9-7
9 Methods — Defining Class Operations
Class Folders
You can define class methods in files that are separate from the class definition file, with certain
exceptions (see “Methods You Must Define in the classdef File” on page 9-10).
To use multiple files for class definitions, put the class files in a folder having a name beginning with
the @ character followed by the name of the class (this is called a class folder). Ensure that the parent
folder of the class folder is on the MATLAB path.
If the class folder is contained in one or more package folders, then the top-level package folder must
be on the MATLAB path.
For example, the folder @MyClass must contain the file MyClass.m (which contains the classdef
block) and contains other methods and function defined in files having a .m extension. The folder
@MyClass can contain a number of files:
@MyClass/MyClass.m
@MyClass/subsref.m
@MyClass/subsasgn.m
@MyClass/horzcat.m
@MyClass/vertcat.m
@MyClass/myFunc.m
MATLAB treats any function file in the class folder as a method of the class. Function files can be
MATLAB code (.m), Live Code file format (.mlx), MEX functions (platform dependent extensions),
and P-code files (.p). The base name of the file must be a valid MATLAB function name. Valid function
names begin with an alphabetic character, and can contain letters, numbers, or underscores.
For information on defining methods as C++ MEX functions, see “Using MEX Functions for MATLAB
Class Methods”.
9-8
Methods in Separate Files
For code readability, declaring the function signature in the classdef file in a methods block is good
practice. You also must declare the function signature to set method attributes. If you do not declare
the function signature in the classdef file, all attributes of the method take their default values. For
example, a method without a signature in classdef has Access set to public, Sealed set to
false, and so on.
For example, the following code shows a method with Access set to private in the methods block.
The method implementation resides in a separate file. Do not include the function or end keywords
in the methods block. Include only the function signature showing input and output arguments.
classdef MyClass
methods (Access = private)
output = myFunc(obj,arg1,arg2)
end
end
To create a static method, set the method Static attribute to true and list the function signature in
a static methods block in the classdef file. Include the input and output arguments with the
function name. For example:
classdef MyClass
...
methods (Static)
output = staticFunc1(arg1,arg2)
staticFunc2
end
...
end
Define the functions in separate files using the same function signature. For example, in the file
@MyClass/staticFunc1.m:
and in @Myclass/staticFunc2.m:
function staticFunc2
...
end
9-9
9 Methods — Defining Class Operations
• Class constructor
• All functions that use dots in their names, including:
• Converter methods that must use the package name as part of the class name because the
class is contained in packages
• Property set and get access methods
Related Information
See Also
Related Examples
• “Folders Containing Class Definitions” on page 6-14
• “Live Code File Format (.mlx)”
• “Call MEX Functions”
• “Using MEX Functions for MATLAB Class Methods”
• “Security Considerations to Protect Your Source Code”
9-10
Method Invocation
Method Invocation
In this section...
“Dot and Function Syntaxes” on page 9-11
“Determining Which Method Is Invoked” on page 9-12
MATLAB classes support both dot and function syntaxes for invoking methods. This topic
demonstrates both syntaxes and describes how MATLAB determines what method to invoke.
For example, dataSetSummary stores a set of numeric data along with the mean, median, and range
of that data. The class defines two methods: showDataSet displays the current data stored in the
data property, and newDataSet replaces the current value of data and calculates the mean, median,
and range of that data.
classdef dataSetSummary
properties (SetAccess=private)
data {mustBeNumeric}
dataMean
dataMedian
dataRange
end
methods
function showDataSet(obj)
disp(obj.data)
end
function obj = newDataSet(obj,inputData)
obj.data = inputData;
obj.dataMean = mean(inputData);
obj.dataMedian = median(inputData);
obj.dataRange = range(inputData);
end
end
end
Create an instance of dataSetSummary and invoke newDataSet to add data to the object. Use dot
syntax to call newDataSet. Because dataSetSummary is a value class, assign the result back to the
original variable to preserve the change.
a = dataSetSummary;
a = a.newDataSet([1 2 3 4])
a =
data: [1 2 3 4]
9-11
9 Methods — Defining Class Operations
dataMean: 2.5000
dataMedian: 2.5000
dataRange: 3
Invoke the showDataSet method, but use function syntax for this call.
showDataSet(a)
1 2 3 4
obj.(expression)
The expression must evaluate to a character vector or string that is the name of a method. For
example, these two statements are equivalent for an object a of class dataSetSummary.
a.showDataSet
a.("showDataSet")
This technique does not work when used with function syntax.
You can dot index into the result of any method that returns a value for which dot indexing is defined,
such as an object property or structure field name. For example, add a new method returnSummary
to the dataSetSummary class that returns all of the stored data in a struct.
Call returnSummary and use dot indexing to return the median of the data set.
a.returnSummary.Median
ans =
2.5000
For more information on indexing into the result of function calls, see “Indexing into Function Call
Results”.
A = classA;
B = classB;
A.plus(B)
9-12
Method Invocation
No other arguments are considered. Methods of other arguments are never called, nor are functions.
In other syntaxes, MATLAB must determine which of possibly many versions of an operator or
function to call in a given situation. The default behavior is to call the method associated with the
leftmost argument. In both of these statements, the plus method defined by classA is called.
objA + objB
plus(objA,objB)
However, this default behavior can be changed when one object has precedence over another.
Object Precedence
Depending on how classes are defined, the objects of those classes can take precedence over other
objects when it comes to method dispatching:
• Classes defined with the classdef syntax have precedence over these MATLAB classes:
double, single, int64, uint64, int32, uint32, int16, uint16, int8, uint8, char, string,
logical, cell, struct, and function_handle.
• Classes defined with the classdef syntax can specify their relative precedence to other classes
using the InferiorClasses attribute.
In “Representing Polynomials with Classes” on page 19-2, the DocPolynom class defines a plus
method that enables the addition of DocPolynom objects. Construct a DocPolynom instance.
p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x - 5
This statement adds a double to the DocPolynom instance. The DocPolynom class is dominant over
the built-in double class, even though the double is the leftmost argument in 1 + p. This code
invokes the DocPolynom plus method to add the polynomials.
1 + p
ans =
x^3 - 2*x - 4
You can also specify the relative precedence of classes defined with the classdef syntax by listing
inferior classes in a class attribute. The InferiorClasses attribute gives the class higher
precedence than the classes listed as arguments for the attribute. Define the InferiorClasses
attribute in the classdef statement:
classdef (InferiorClasses = {?class1,?class2}) myClass
This attribute establishes a relative priority of the class being defined with the order of the classes
listed. For more information, see “Class Precedence” on page 6-19.
See Also
More About
• “Class Attributes” on page 6-5
9-13
9 Methods — Defining Class Operations
9-14
Class Constructor Methods
In this section...
“Purpose of Class Constructor Methods” on page 9-15
“Basic Structure of Constructor Methods” on page 9-15
“Guidelines for Constructors” on page 9-16
“Default Constructor” on page 9-17
“When to Define Constructors” on page 9-17
“Related Information” on page 9-17
“Initializing Objects in Constructor” on page 9-17
“No Input Argument Constructor Requirement” on page 9-18
“Subclass Constructors” on page 9-18
“Implicit Call to Inherited Constructor” on page 9-21
“Errors During Class Construction” on page 9-21
“Output Object Suppressed” on page 9-22
MATLAB classes that do not explicitly define any class constructors have a default constructor
method. This method returns an object of the class that is created with no input arguments. A class
can define a constructor method that overrides the default constructor. An explicitly defined
constructor can accept input arguments, initialize property values, call other methods, and perform
other operations necessary to create objects of the class.
9-15
9 Methods — Defining Class Operations
%% Pre Initialization %%
% Any code not using output argument (obj)
if nargin == 0
% Provide values for superclass constructor
% and initialize other inputs
a = someDefaultValue;
args{1} = someDefaultValue;
args{2} = someDefaultValue;
else
% When nargin ~= 0, assign to cell array,
% which is passed to supclass constructor
args{1} = b;
args{2} = c;
end
compvalue = myClass.staticMethod(a);
%% Object Initialization %%
% Call superclass constructor before accessing object
% You cannot conditionalize this statement
obj = obj@BaseClass1(args{:});
%% Post Initialization %%
% Any code, including access to object
obj.classMethod(arg);
obj.ComputedValue = compvalue;
...
end
...
end
...
end
Call the constructor like any function, passing arguments and returning an object of the class.
obj = ConstructorDesign(a,b,c);
9-16
Class Constructor Methods
Default Constructor
If a class does not define a constructor, MATLAB supplies a default constructor that takes no
arguments and returns a scalar object whose properties are initialized to property default values. The
default constructor supplied by MATLAB also calls all superclass constructors with no arguments or
with any argument passed to the default subclass constructor.
When a subclass does not define a constructor, the default constructor passes its inputs to the direct
superclass constructor. This behavior is useful when there is no need for a subclass to define a
constructor, but the superclass constructor does require input arguments.
• Input arguments
• Initializing object state, such as property values, for each instance of the class
• Calling the superclass constructor with values that are determined by the subclass constructor
Related Information
For information specific to constructing enumerations, see “Enumeration Class Constructor Calling
Sequence” on page 14-7.
For information on creating object arrays in the constructor, see “Create and Initialize Object Arrays”
on page 10-2.
If the class being created is a subclass, MATLAB calls the constructor of each superclass class to
initialize the object. Implicit calls to the superclass constructor are made with no arguments. If
superclass constructors require arguments, call them from the subclass constructor explicitly. See
“Control Sequence of Constructor Calls” on page 12-11
For example, the following constructor can assign the value of the object's property A as the first
statement because the object obj has already been assigned to an instance of MyClass.
function obj = MyClass(a,b,c)
obj.A = a;
...
end
You can call other class methods from the constructor because the object is already initialized.
The constructor also creates an object whose properties have their default values — either empty
([]) or the default value specified in the property definition block.
For example, this constructor operates on the input arguments to assign the value of the Value
property.
9-17
9 Methods — Defining Class Operations
When initializing the object, for example, by assigning values to properties, use the name of the
output argument to refer to the object within the constructor. For example, in the following code the
output argument is obj and the object is reference as obj:
For more information on defining default property values, see “Define Properties with Default Values”
on page 8-13.
• When loading objects into the workspace, if the class ConstructOnLoad attribute is set to true,
the load function calls the class constructor with no arguments.
• When creating or expanding an object array such that not all elements are given specific values,
the class constructor is called with no arguments to fill in unspecified elements (for example,
x(10,1) = MyClass(a,b,c);). In this case, the constructor is called once with no arguments
to populate the empty array elements (x(1:9,1)) with copies of this one object.
If there are no input arguments, the constructor creates an object using only default properties
values. A good practice is to add a check for zero arguments to the class constructor to prevent an
error if either of these two cases occur:
For ways to handle superclass constructors, see “Basic Structure of Constructor Methods” on page 9-
15.
Subclass Constructors
Subclass constructors can call superclass constructors explicitly to pass arguments to the superclass
constructor. The subclass constructor must specify these arguments in the call to the superclass
constructor and must use the constructor output argument to form the call. Here is the syntax:
9-18
Class Constructor Methods
The subclass constructor must make all calls to superclass constructors before any other references
to the object (obj). This restriction includes assigning property values or calling ordinary class
methods. Also, a subclass constructor can call a superclass constructor only once.
If the classdef does not specify the class as a superclass, the constructor cannot call a superclass
constructor with this syntax. That is, subclass constructor can call only direct superclass constructors
listed in the classdef line.
classdef MyClass < SuperClass1 & SuperClass2
MATLAB calls any uncalled constructors in the left-to-right order in which they are specified in the
classdef line. MATLAB passes no arguments with these calls.
Calls to superclass constructors must be unconditional. There can be only one call for a given
superclass. Initialize the superclass portion of the object by calling the superclass constructors before
using the object (for example, to assign property values or call class methods).
To call a superclass constructor with different arguments that depend on some condition, build a cell
array of arguments and provide one call to the constructor.
For example, the Cube class constructor calls the superclass Shape constructor using default values
when the Cube constructor is called with no arguments. If the Cube constructor is called with four
input arguments, then pass upvector and viewangle to the superclass constructor:
classdef Cube < Shape
properties
SideLength = 0
Color = [0 0 0]
end
methods
function cubeObj = Cube(length,color,upvector,viewangle)
% Assemble superclass constructor arguments
if nargin == 0
super_args{1} = [0 0 1];
super_args{2} = 10;
elseif nargin == 4
super_args{1} = upvector;
super_args{2} = viewangle;
else
error('Wrong number of input arguments')
end
9-19
9 Methods — Defining Class Operations
To support a syntax that calls the superclass constructor with no arguments, provide this syntax
explicitly.
Suppose in the case of the Cube class example, all property values in the Shape superclass and the
Cube subclass have default values specified in the class definitions. Then you can create an instance
of Cube without specifying any arguments for the superclass or subclass constructors.
Here is how you can implement this behavior in the Cube constructor:
methods
function cubeObj = Cube(length,color,upvector,viewangle)
% Assemble superclass constructor arguments
if nargin == 0
super_args = {};
elseif nargin == 4
super_args{1} = upvector;
super_args{2} = viewangle;
else
error('Wrong number of input arguments')
end
More on Subclasses
See “Design Subclass Constructors” on page 12-7 for information on creating subclasses.
9-20
Class Constructor Methods
For example, the following class constructor requires one input argument (a datetime object), which
the constructor assigns to the CurrentDate property.
classdef BaseClassWithConstr
properties
CurrentDate datetime
end
methods
function obj = BaseClassWithConstr(dt)
obj.CurrentDate = dt;
end
end
end
Suppose that you create a subclass of BaseClassWithConstr, but your subclass does not require an
explicit constructor method.
You can construct an object of the SubclassDefaultConstr by calling its default constructor with
the superclass argument:
obj = SubclassDefaultConstr(datetime);
For information on subclass constructors, see “Subclass Constructors” on page 9-18 and “Default
Constructor” on page 9-17.
MATLAB calls the delete method on the object, the delete methods for any objects contained in
properties, and the delete methods for any initialized base classes.
Depending on when the error occurs, MATLAB can call the class destructor before the object is fully
constructed. Therefore class delete methods must be able to operate on partially constructed
objects that might not have values for all properties. For more information, see “Support Destruction
of Partially Constructed Objects” on page 7-15.
For information on how objects are destroyed, see “Handle Class Destructor” on page 7-13.
9-21
9 Methods — Defining Class Operations
Use nargout to determine if the constructor has been called with an output argument. For example,
the class constructor for the MyApp class clears the object variable, obj, if called with no output
assigned:
classdef MyApp
methods
function obj = MyApp
...
if nargout == 0
clear obj
end
end
...
end
end
When a class constructor does not return an object, MATLAB does not trigger the meta.class
InstanceCreated event.
See Also
Related Examples
• “Simplifying the Interface with a Constructor” on page 3-16
• “Subclass Constructor Implementation” on page 12-8
9-22
Static Methods
Static Methods
In this section...
“What Are Static Methods” on page 9-23
“Why Define Static Methods” on page 9-23
“Defining Static Methods” on page 9-23
“Calling Static Methods” on page 9-23
“Inheriting Static Methods” on page 9-24
Static methods are useful when you do not want to create an instance of the class before executing
some code. For example, suppose you want to set up the MATLAB environment or use the static
method to calculate data required to create class instances.
Suppose that a class needs a value for pi calculated to particular tolerances. The class could define its
own version of the built-in pi function for use within the class. This approach maintains the
encapsulation of the class's internal workings, but does not require an instance of the class to return
a value.
Calling the pi method of MyClass in the previous section would require this statement:
value = MyClass.pi(.001);
You can also invoke static methods using an instance of the class, like any method:
9-23
9 Methods — Defining Class Operations
obj = MyClass;
value = obj.pi(.001);
See Also
Related Examples
• “Implementing the AccountManager Class” on page 3-11
9-24
Overload Functions in Class Definitions
In this section...
“Why Overload Functions” on page 9-25
“Implementing Overloaded MATLAB Functions” on page 9-25
“Rules for Naming to Avoid Conflicts” on page 9-27
You can also modify default behaviors by implementing specific functions that control these
behaviors. For more information on functions that modify default behaviors, see “Methods That
Modify Default Behavior” on page 17-2.
MATLAB uses the dominant argument to determine which version of a function to call. If the
dominant argument is an object, then MATLAB calls the method defined by the object's class, if one
exists.
In cases where a class defines a method with the same name as a global function, the class's
implementation of the function is said to overload the original global implementation.
• Define a method with the same name as the function you want to overload.
• Ensure that the method argument list accepts an object of the class, which MATLAB uses to
determine which version to call.
• Perform the necessary steps in the method to implement the function. For example, access the
object properties to manipulate data.
Generally, the method that overloads a function produces results similar to the MATLAB function.
However, there are no requirements regarding how you implement the overloading method. The
overloading method does not need to match the signature of the overloaded function.
Note MATLAB does not support defining multiple methods with the same name but different
signatures in the same class.
9-25
9 Methods — Defining Class Operations
It is convenient to overload commonly used functions to work with objects of your class. For example,
suppose that a class defines a property that stores data that you often graph. The MyData class
overrides the bar function and adds a title to the graph:
classdef MyData
properties
Data
end
methods
function obj = MyData(d)
if nargin > 0
obj.Data = d;
end
end
function bar(obj)
y = obj.Data;
bar(y,'EdgeColor','r');
title('My Data Graph')
end
end
end
The MyData bar method has the same name as the MATLAB bar function. However, the MyData bar
method requires a MyData object as input. Because the method is specialized for MyData objects, it
can extract the data from the Data property and create a specialized graph.
y = rand(1,10);
md = MyData(y);
bar(md)
md.bar
Classes designed to implement new MATLAB data types typically define certain operators, such as
addition, subtraction, or equality.
For example, standard MATLAB addition (+) cannot add two polynomials because this operation is not
defined by simple addition. However, a polynomial class can define its own plus method that the
MATLAB language calls to perform addition of polynomial objects when you use the + symbol:
p1 + p2
9-26
Overload Functions in Class Definitions
• You can reuse names that you have used in unrelated classes.
• You can reuse names in subclasses if the member does not have public or protected access. These
names then refer to entirely different methods, properties, and events without affecting the
superclass definitions
• Within a class, all names exist in the same name space and must be unique. A class cannot define
two methods with the same name and a class cannot define a local function with the same name as
a method.
• The name of a static method is considered without its class prefix. Thus, a static method name
without its class prefix cannot match the name of any other method.
See Also
Related Examples
• “Class Support for Array-Creation Functions” on page 9-28
9-27
9 Methods — Defining Class Operations
Class support for any of the array-creation functions enables you to develop code that you can share
with built-in and user-defined data types. For example, the class of the variable x in the following
code can be a built-in type during initial development, and then be replaced by a user-defined class
that transparently overloads zeros:
cls = class(x);
zArray = zeros(m,n,cls);
• Class name syntax — Specify class name that determines the type of array elements.
• Prototype object syntax — Provide a prototype object that the function uses to determine the type
and other characteristics of the array elements.
For example:
zArray = zeros(2,3,'uint8');
p = uint8([1 3 5; 2 4 6]);
zArray = zeros(2,3,'like',p);
After adding support for these functions to a class named MyClass, you can use similar syntax with
that class:
zArray = zeros(2,3,'MyClass');
p = MyClass(...);
zArray = zeros(size(p),'like',p);
MATLAB uses these arguments to dispatch to the appropriate method in your class.
Array-Creation Functions
ones
9-28
Class Support for Array-Creation Functions
Array-Creation Functions
zeros
eye
NaN
Inf
true
false
cast
rand
randn
randi
These functions also support similar overloading, with the exception that the output is always a scalar
(or a 1-by-1 sparse matrix):
Scalar Functions
eps
realmax
realmin
intmax
intmin
flintmax
For these functions, you do not need to specify the size when creating scalars of a specific type. For
example:
d = eps('single');
p = single([1 3 5; 2 4 6]);
d = eps('like',p);
After adding support for these functions to a user-defined class, you can use similar syntax with that
class as well.
To create an array of objects with specific property values or if the constructor needs other inputs,
use the prototype object to provide this information.
Classes can support both the class name and the prototype object syntax.
You can implement a class name syntax with the true and false functions even though these
functions do not support that syntax by default.
9-29
9 Methods — Defining Class Operations
If your class implements a class name syntax, but does not implement a prototype object syntax for a
particular function, you can still call both syntaxes. For example, if you implement a static zeros
method only, you can call:
zeros(...,'like',MyClass(...))
In the case in which you call the prototype object syntax, MATLAB first searches for a method named
zerosLike. If MATLAB cannot find this method, it calls for the zeros static method.
This feature is useful if you only need the class name to create the array. You do not need to
implement both methods to support the complete array-creation function syntax. When you
implement only the class name syntax, a call to a prototype object syntax is the same as the call to the
class name syntax.
As a Static method:
methods (Static)
function z = zeros(varargin)
...
end
end
• Implement the prototype object syntax:
zeros(...,'like',obj)
As a Hidden method with the char vector 'Like' appended to the name.
methods (Hidden)
function z = zerosLike(obj,varargin)
...
end
end
The special support for array-creation functions results from the interpretation of the syntax.
9-30
Class Support for Array-Creation Functions
The input arguments to an array-creation function can include the dimensions of the array the
function returns and possibly other arguments. In general, there are three cases that your methods
must support:
When the array-creation function calls your class method, it passes the input arguments, excluding
the class name or the literal 'like' and the object variable to your method. You can implement your
methods with these signatures:
Sample Class
The Color class represents a color in a specific color space, such as, RGB, HSV, and so on. The
discussions in “Class Name Method Implementations” on page 9-31 and “Prototype Object Method
Implementation” on page 9-33 use this class as a basis for the overloaded method implementations.
classdef Color
properties
ColorValues = [0,0,0]
ColorSpace = 'RGB'
end
methods
function obj = Color(cSpace,values)
if nargin > 0
obj.ColorSpace = cSpace;
obj.ColorValues = values;
end
end
end
end
The zeros function strips the final ClassName char vector and uses it to form the call to the static
method in the Color class. The arguments passed to the static method are the array dimension
arguments.
9-31
9 Methods — Defining Class Operations
Here is an implementation of a zeros method for the Color class. This implementation:
classdef Color
...
methods (Static)
function z = zeros(varargin)
if (nargin == 0)
% For zeros('Color')
z = Color;
elseif any([varargin{:}] <= 0)
% For zeros with any dimension <= 0
z = Color.empty(varargin{:});
else
% For zeros(m,n,...,'Color')
% Use property default values
z = repmat(Color,varargin{:});
end
end
end
end
The zeros method uses default values for the ColorValues property because these values are
appropriate for this application. An implementation of a ones method can set the ColorValues
property to [1,1,1], for example.
Suppose that you want to overload the randi function to achieve the following objectives:
• Define each ColorValue property as a 1-by-3 array in the range of 1 to a specified maximum
value (for example, 1–255).
• Accommodate scalar, empty, and multidimensional array sizes.
• Return an array of Color objects of the specified dimensions, each with random ColorValues.
classdef Color
...
methods (Static)
function r = randi(varargin)
if (nargin == 0)
% For randi('ClassName')
r = Color('RGB',randi(255,[1,3]));
elseif any([varargin{2:end}] <= 0)
% For randi with any dimension <= 0
r = Color.empty(varargin{2:end});
else
% For randi(max,m,n,...,'ClassName')
if numel([varargin{:}]) < 2
error('Not enough input arguments')
end
dims = [varargin{2:end}];
r = zeros(dims,'Color');
9-32
Class Support for Array-Creation Functions
for k = 1:prod(dims)
r(k) = Color('RGB',randi(varargin{1},[1,3]));
end
end
end
end
end
The objective of a method that returns an array of objects that are “like a prototype object” depends
on the requirements of the class. For the Color class, the zeroLike method creates objects that
have the ColorSpace property value of the prototype object, but the ColorValues are all zero.
Here is an implementation of a zerosLike method for the Color class. This implementation:
Note In actual practice, the Color class requires error checking, color space conversions, and so on.
This overly simplified version illustrates the implementation of the overloaded methods.
classdef Color
properties
ColorValues = [0,0,0]
ColorSpace = 'RGB'
end
methods
function obj = Color(cSpace,values)
9-33
9 Methods — Defining Class Operations
if nargin > 0
obj.ColorSpace = cSpace;
obj.ColorValues = values;
end
end
end
methods (Static)
function z = zeros(varargin)
if (nargin == 0)
% For zeros('ClassName')
z = Color;
elseif any([varargin{:}] <= 0)
% For zeros with any dimension <= 0
z = Color.empty(varargin{:});
else
% For zeros(m,n,...,'ClassName')
% Use property default values
z = repmat(Color,varargin{:});
end
end
function r = randi(varargin)
if (nargin == 0)
% For randi('ClassName')
r = Color('RGB',randi(255,[1,3]));
elseif any([varargin{2:end}] <= 0)
% For randi with any dimension <= 0
r = Color.empty(varargin{2:end});
else
% For randi(max,m,n,...,'ClassName')
if numel([varargin{:}]) < 2
error('Not enough input arguments')
end
dims = [varargin{2:end}];
r = zeros(dims,'Color');
for k = 1:prod(dims)
r(k) = Color('RGB',randi(varargin{1},[1,3]));
end
end
end
end
methods (Hidden)
function z = zerosLike(obj,varargin)
if nargin == 1
% For zeros('like',obj)
cSpace = obj.ColorSpace;
z = Color;
z.ColorSpace = cSpace;
elseif any([varargin{:}] <= 0)
% For zeros with any dimension <= 0
z = Color.empty(varargin{:});
else
% For zeros(m,n,...,'like',obj)
if ~isscalar(obj)
error('Prototype object must be scalar')
end
obj = Color(obj.ColorSpace,zeros(1,3,'like',obj.ColorValues));
z = repmat(obj,varargin{:});
end
end
end
end
See Also
Related Examples
• “Create and Initialize Object Arrays” on page 10-2
9-34
Class Methods for Graphics Callbacks
uicontrol('Style','slider','Callback',@obj.sliderCallback)
Where your class defines a method called sliderCallback and obj is an instance of your class.
To use a static method as a callback, specify the callback property as a function handle that includes
the class name that is required to refer to a static method:
uicontrol('Style','slider','Callback',@MyClass.sliderCallback)
@obj.methodName
function methodName(obj,src,eventData)
...
end
For static methods, the required class name ensures MATLAB dispatches to the method of the
specified class:
@MyClass.methodName
Define the static callback method with two input arguments — the event source handle and the event
data
function methodName(src,eventData)
9-35
9 Methods — Defining Class Operations
If you want to pass arguments to your callback in addition to the source and event data arguments
passed by MATLAB, you can use an anonymous function. The basic syntax for an anonymous function
that you assign to the graphic object's Callback property includes the object as the first argument:
@(src,event)callbackMethod(object,src,eventData,arg1,...argn)
function methodName(obj,src,eventData,varargin)
...
end
The SeaLevelSlider class creates a slider that varies the color limits of an indexed image to give
the illusion of varying the sea level.
Class Definition
• The class properties store figure and axes handles and the calculated color limits.
• The class constructor creates the graphics objects and assigns the slider callback.
• The callback function for the slider accepts the three required arguments — a class instance, the
handle of the event source, and the event data. The event data argument is empty and not used.
• The uicontrol callback uses dot notation to reference the callback
method: ...'Callback',@obj.sliderCallback.
methods
function obj = SeaLevelSlider(x,map)
obj.Figure = figure('Colormap',map,...
'Position',[100,100,560,580],...
'Resize','off');
obj.Axes = axes('DataAspectRatio',[1,1,1],...
'XLimMode','manual','YLimMode','manual',...
'Parent',obj.Figure);
image(x,'CDataMapping','scaled',...
'Parent',obj.Axes);
obj.CLimit = get(obj.Axes,'CLim');
uicontrol('Style','slider',...
'Parent',obj.Figure,...
'Max',obj.CLimit(2)-10,...
'Min',obj.CLimit(1)-1,...
'Value',obj.CLimit(1),...
9-36
Class Methods for Graphics Callbacks
'Units','normalized',...
'Position',[0.9286,0.1724,0.0357,0.6897],...
'SliderStep',[0.003,0.005],...
'Callback',@obj.sliderCallback);
end
function sliderCallback(obj,src,~)
minVal = get(src,'Value');
maxVal = obj.CLimit(2);
obj.Axes.CLim = [minVal maxVal];
end
end
end
The class uses the cape image that is included with the MATLAB product. To obtain the image data,
use the load command:
After loading the data, create a SeaLevelSlider object for the image:
slaObj = SeaLevelSlider(X,map);
Move the slider to change the color mapping and visualize a rise in sea level.
9-37
9 Methods — Defining Class Operations
See Also
More About
• “Listener Callback Syntax” on page 11-24
9-38
10
Object Arrays
You can create arrays of class-based objects with indexing behavior similar to built-in types like
double or string. However, creating object arrays requires particular attention to how the objects
are constructed.
classdef SimpleValue
properties
prop1 = 0
end
end
for k = 1:5
objArray(k) = SimpleValue;
end
Reference and assign the property values of specific objects using array indexing. For example,
assign the value of 5 to the prop1 property of objArray(1).
objArray(1).prop1 = 5;
Return the values of prop1 for all the objects and assign them to a vector.
p = [objArray.prop1]
p =
5 0 0 0 0
For more information on accessing properties and methods of an object array, see “Accessing
Properties and Methods in Object Arrays” on page 10-6.
a(2,2) = SimpleValue
10-2
Create and Initialize Object Arrays
a =
prop1
When you create an object array this way, MATLAB takes one of two approaches to fill in the rest of
the elements of the array, depending on whether the class is value or handle.
After you construct the last element in an array of value class objects, MATLAB:
If you want to initialize property values using an input to the constructor, you must also have a no-
argument option in the constructor.
For example, add a constructor to SimpleValue that accepts an input and assigns it that input to
prop1.
classdef SimpleValue
properties
prop1 = 0
end
methods
function obj = SimpleValue(v)
if nargin > 0
obj.prop1 = v;
end
end
end
end
b(5) = SimpleValue(7);
Return all of the prop1 values and assign them to a vector y. The object in b(5) has a prop1 value
of 7. MATLAB calls the no-argument constructor once and copies that value to all the remaining
elements in the array. Those elements have the initial prop1 value of 0.
y = [b.prop1]
y =
0 0 0 0 7
If the SimpleValue constructor cannot handle no-argument calls, MATLAB errors when trying to
populate b(1) through b(4).
After you construct the last element in an array of handle class objects, MATLAB:
10-3
10 Object Arrays
• Copies the property values from the last object in the array
• Creates objects for the rest of the array using those copied values, without calling the constructor
Unlike the case for value classes, MATLAB does not call the constructor to populate the array, so all
objects in that array start with the same property values. For example, define a handle class that
generates random numbers for its property.
Create a new 1-by-5 array by constructing c(5), and verify the prop1 values. To create the array,
MATLAB calls the constructor twice. The first time, MATLAB assigns a value to c(5).RandNum. The
second time, it assigns the result of a second call to randi to create c(1). Finally, it copies this value
of RandNum to the rest of the elements in c.
c(5) = InitHandleArray;
z = [c.RandNum]
z =
91 91 91 91 82
Although MATLAB does not need a no-argument option in the constructor to create a handle class
array, having a no-argument option is still the recommended practice for all class constructors.
classdef ObjectArray
properties
Value
end
methods
function obj = ObjectArray(F)
if nargin ~= 0
m = size(F,1);
n = size(F,2);
p(m,n) = obj;
for i = 1:m
for j = 1:n
p(i,j).Value = F(i,j);
end
end
obj = p;
end
10-4
Create and Initialize Object Arrays
end
end
end
This pattern for the constructor works for both value and handle objects as long as the no-argument
option is included.
See Also
Related Examples
• “Accessing Properties and Methods in Object Arrays” on page 10-6
• “Class Constructor Methods” on page 9-15
10-5
10 Object Arrays
classdef BasicClass
properties
prop1 {mustBeNumeric} = 0
end
methods
function r = roundOff(obj)
r = round([obj.prop1],2);
end
function r = multiplyBy(obj,n)
r = [obj.prop1]*n;
end
end
end
Create a BasicClass object array with a loop. The loop assigns the value of i to prop1 of each
array element a(i). For more information, see “Create and Initialize Object Arrays” on page 10-2.
for i = 1:4
a(i) = BasicClass;
a(i).prop1 = i;
end
a.prop1
ans =
ans =
ans =
ans =
To collect the comma-separated list in a vector and assign it to a variable, enclose the object array
expression in brackets.
10-6
Accessing Properties and Methods in Object Arrays
prop1Values = [a.prop1]
prop1Values =
1 2 3 4
Note Dynamic properties are defined per object and are therefore not properties of the class.
Because of this distinction, even if every element of the array defines the same dynamic property,
MATLAB returns an error if you try to access the dynamic properties of an entire array in a single
statement. Index into each object individually to access dynamic property values. For information
about classes that can define dynamic properties, see “Dynamic Properties — Adding Properties to an
Instance” on page 8-47.
You can reference the property values of individual elements in an object array using standard
MATLAB indexing syntax. For example, return the value of prop1 for the object a(2).
a(2).prop1
ans =
You can also return a subset of prop1 values from the object array.
prop1partialValues = [a(2:3).prop1]
prop1partialValues =
2 3
propValues =
Assign property values of individual array elements using standard indexing syntax. For example,
assign a new value to a(2).prop1 and verify the result.
a(2).prop1 = 1.7321;
a(2).prop1
ans =
1.7321
10-7
10 Object Arrays
roundOff(a)
ans =
Class methods must be designed to handle the size and shape of the object arrays you need to create.
In BasicClass, the roundOff and multiplyBy methods are designed to handle arrays of any size
by enclosing the property references in square brackets.
r = round([obj.prop1],2);
r = [obj.prop1]*n;
A method that only works on scalar instances errors if passed a nonscalar array. For more
information, see “Vectorize Methods” on page 2-4.
You can also use standard function notation to invoke a method for one element of the object array.
For example, call the roundOff method on a(3).
roundOff(a(3))
ans =
2.7200
Use a similar syntax for a method that takes one or more input arguments.
multiplyBy(a(3),5)
ans =
13.5915
See Also
Related Examples
• “Creating a Simple Class” on page 2-2
• “Property Syntax” on page 8-4
• “Method Invocation” on page 9-11
10-8
Implicit Class Conversion
To perform the conversion, MATLAB attempts to use a converter method, the constructor of the
destination class, or the cast function, depending on the context of the conversion.
Concatenation
In concatenation operations, the dominant object determines the class of the resulting array. MATLAB
determines the dominant object according to these rules:
MATLAB first tries to call a converter method. If no converter method is found, it calls the
constructor for ClassA. The concatenation statement is equivalent to:
C = [A,ClassA(B)]
If the call to the ClassA constructor fails to convert B to ClassA, MATLAB issues an error. If the
conversion succeeds, MATLAB concatenates A with the converted B.
Subscripted Assignment
In subscripted assignment, the left side of the assignment statement defines the class of the array. If
you assign array elements when the right side is a different class than the left side, MATLAB attempts
to convert the right side to the class of the left side.
A = ClassA;
B = ClassB;
A(2) = B;
10-9
10 Object Arrays
MATLAB attempts to perform the conversion by first looking for a converter method. If no converter
method is found, it calls the ClassA constructor. The assignment is then equivalent to:
A(2) = ClassA(B)
A(2) = cast(B,"like",A)
If the conversion still fails after these steps, MATLAB issues an error. If the conversion succeeds,
MATLAB assigns the converted value to A(2).
Property Validation
When you assign a value to a property that specifies a class restriction as part of its validation,
MATLAB uses the built-in function isa to check the relationship between the value and the class. If
the value is not the specified class or one of its subclasses, MATLAB attempts to convert the value to
the specified class.
classdef ClassA
properties
Prop ClassB
end
end
classdef ClassB
end
classdef ClassC
end
This script shows when MATLAB attempts conversions during property assignments.
A = ClassA;
B = ClassB;
SB = SubClassB;
C = ClassC;
A.Prop = B; % no conversion
A.Prop = SB; % no conversion
A.Prop = C; % conversion required
In this script:
• A.Prop = B does not require conversion because B belongs to ClassB, which satisfies the
property validation for Prop defined in ClassA.
• A.Prop = SB does not require conversion because SB belongs to SubClassB, which is a subclass
of ClassB.
• A.Prop = C requires conversion because C does not belong to ClassB or its subclass
SubClassB.
10-10
Implicit Class Conversion
MATLAB attempts to convert C to ClassB or its subclass SubClassB by first calling ClassB(C).
This call can invoke a converter method called ClassB (defined by ClassC) or the ClassB
constructor. If calling ClassB(C) does not yield an instance of ClassB or SubClassB, MATLAB
attempts to cast the result of ClassB(C) to ClassB as a final step.
If these steps fail to convert C to ClassB or SubClassB, MATLAB issues an error. If the
conversion succeeds, MATLAB writes the converted value to A.Prop.
Note Property class validation does not support implicit conversion of any built-in types to cell
arrays, even if you provide your own conversion method.
To demonstrate the conversion process, refer to these class and function definitions.
classdef ClassA
end
classdef ClassB
end
function test(x)
arguments
x ClassA
end
end
This script shows when MATLAB attempts conversions during argument validation.
A = ClassA;
SA = SubClassA;
B = ClassB;
test(A); % no conversion
test(SA); % no conversion
test(B); % conversion required
In this script:
• test(A) does not require conversion because A belongs to ClassA, which satisfies the argument
validation for x defined in test.
• test(SA) does not require conversion because SA belongs to SubClassA, which is a subclass of
ClassA.
• test(B) requires conversion because B does not belong to ClassA or its subclass SubClassA.
MATLAB attempts to convert B to ClassA or its subclass SubClassA by first calling ClassA(B).
This call can invoke a converter method called ClassA (defined by ClassB) or the ClassA
10-11
10 Object Arrays
constructor. If calling ClassA(B) does not yield an instance of ClassA or SubClassA, MATLAB
attempts to cast the result of ClassA(B) to ClassA as a final step.
If these steps fail to convert B to ClassA or SubClassA, MATLAB issues an error. If the
conversion succeeds, MATLAB uses the converted value for the function call.
Note Argument validation does not support implicit conversion of any built-in types to cell arrays,
even if you provide your own conversion method.
See Also
Related Examples
• “Valid Combinations of Unlike Classes”
• “Concatenating Objects of Different Classes” on page 10-13
• “Object Converters” on page 17-5
• “Function Argument Validation”
10-12
Concatenating Objects of Different Classes
In this section...
“Basic Knowledge” on page 10-13
“MATLAB Concatenation Rules” on page 10-13
“Concatenating Objects” on page 10-13
“Calling the Dominant-Class Constructor” on page 10-14
“Converter Methods” on page 10-15
Basic Knowledge
The material presented in this section builds on an understanding of the information presented in the
following sections.
It is possible for the dominant class to define horzcat, vertcat, or cat methods that modify the
default concatenation process.
Note MATLAB does not convert objects to a common superclass unless those objects are part of a
heterogeneous hierarchy. For more information, see “Designing Heterogeneous Class Hierarchies” on
page 10-18.
Concatenating Objects
Concatenation combines objects into arrays. For example:
ary = [obj1,obj2,obj3];
10-13
10 Object Arrays
The class of the arrays is the same as the class of the objects being concatenated. Concatenating
objects of different classes is possible if MATLAB can convert objects to the dominant class. MATLAB
attempts to convert unlike objects by:
If conversion of the inferior object is successful, MATLAB returns an array that is of the dominant
class. If conversion is not possible, MATLAB returns an error.
If the constructor simply assigns this argument to a property, the result is an object of the dominant
class with an object of an inferior class stored in a property. If this assignment is not a desired result,
then ensure that class constructors include adequate error checking.
For example, consider the class ColorClass and two subclasses, RGBColor and HSVColor:
classdef ColorClass
properties
Color
end
end
The class RGBColor inherits the Color property from ColorClass. RGBColor stores a color value
defined as a three-element vector of red, green, and blue (RGB) values. The constructor does not
restrict the value of the input argument. It assigns this value directly to the Color property.
classdef RGBColor < ColorClass
methods
function obj = RGBColor(rgb)
if nargin > 0
obj.Color = rgb;
end
end
end
end
The class HSVColor also inherits the Color property from ColorClass. HSVColor stores a color
value defined as a three-element vector of hue, saturation, brightness value (HSV) values.
classdef HSVColor < ColorClass
methods
function obj = HSVColor(hsv)
if nargin > 0
obj.Color = hsv;
end
end
end
end
10-14
Concatenating Objects of Different Classes
Create an instance of each class and concatenate them into an array. The RGBColor object is
dominant because it is the leftmost object and neither class defines a dominance relationship:
crgb = RGBColor([1 0 0]);
chsv = HSVColor([0 1 1]);
ary = [crgb,chsv];
class(ary)
ans =
RGBColor
You can combine these objects into an array because MATLAB can pass the inferior object of class
HSVColor to the constructor of the dominant class. However, notice that the Color property of the
second RGBColor object in the array actually contains an HSVColor object, not an RGB color
specification:
ary(2).Color
ans =
Color: [0 1 1]
Converter Methods
If your class design requires object conversion, implement converter methods for this purpose.
The ColorClass class defines converter methods for RGBColor and HSVColor objects:
classdef ColorClass
properties
Color
end
methods
function rgbObj = RGBColor(obj)
if isa(obj,'HSVColor')
rgbObj = RGBColor(hsv2rgb(obj.Color));
end
end
function hsvObj = HSVColor(obj)
if isa(obj,'RGBColor')
hsvObj = HSVColor(rgb2hsv(obj.Color));
end
end
end
end
Create an array of RGBColor and HSVColor objects with the revised superclass:
crgb = RGBColor([1 0 0]);
chsv = HSVColor([0 1 1]);
10-15
10 Object Arrays
ary = [crgb,chsv];
class(ary)
ans =
RGBColor
MATLAB calls the converter method for the HSVColor object, which it inherits from the superclass.
The second array element is now an RGBColor object with an RGB color specification assigned to the
Color property:
ary(2)
ans =
Color: [1 0 0]
ary(2).Color
ans =
1 0 0
If the leftmost object is of class HSVColor, the array ary is also of class HSVColor, and MATLAB
converts the Color property data to HSV color specification.
ary =
1x2 HSVColor
Properties:
Color
ary(2).Color
ans =
0 1 1
Defining a converter method in the superclass and adding better argument checking in the subclass
constructors produces more predictable results. Here is the RGBColor class constructor with
argument checking:
10-16
Concatenating Objects of Different Classes
obj.Color = rgb;
end
end
end
Your applications can require additional error checking and other coding techniques. The classes in
these examples are designed only to demonstrate concepts.
See Also
More About
• “Implicit Class Conversion” on page 10-9
• “Object Converters” on page 17-5
• “Hierarchies of Classes — Concepts” on page 12-2
10-17
10 Object Arrays
In this section...
“Creating Classes That Support Heterogeneous Arrays” on page 10-18
“MATLAB Arrays” on page 10-18
“Heterogeneous Hierarchies” on page 10-18
“Heterogeneous Arrays” on page 10-19
“Heterogeneous Array Concepts” on page 10-19
“Nature of Heterogeneous Arrays” on page 10-20
“Unsupported Hierarchies” on page 10-22
“Default Object” on page 10-23
“Conversion During Assignment and Concatenation” on page 10-24
“Empty Arrays of Heterogeneous Abstract Classes” on page 10-24
For an example that uses heterogeneous arrays, see “A Class Hierarchy for Heterogeneous Arrays” on
page 20-2.
MATLAB Arrays
MATLAB determines the class of an array by the class of the objects contained in the array. MATLAB
is unlike some languages in which you define an array of object pointers or references. In these other
languages, the type of the array is different from the type of an object in the array. You can access the
elements of the array and dispatch to methods on those elements, but you cannot call an object
method on the whole array, as you can in MATLAB.
Object arrays in MATLAB are homogeneous in class. Because of this homogeneity, you can perform
operations on whole arrays, such as multiplying numeric matrices. You can form heterogeneous
arrays by defining a hierarchy of classes that derive from a common superclass. Cell arrays provide
option for an array type that can hold different kinds of unrelated objects.
Heterogeneous Hierarchies
You can form arrays of objects that are subclasses of a common superclass when these classes are
part of a heterogeneous hierarchy. A MATLAB heterogeneous class hierarchy:
10-18
Designing Heterogeneous Class Hierarchies
For example, in the following diagram, Shape is the root of the heterogeneous hierarchy.
Heterogeneous Arrays
A heterogeneous array is an array of objects that differ in their specific class, but all objects derive
from or are instances of a common superclass. The common superclass forms the root of the
hierarchy of classes that you can combine into heterogeneous arrays.
The common superclass must derive from matlab.mixin.Heterogeneous. Methods that you can
call on the array as a whole must have the same definitions for all subclasses.
• Create arrays of objects that are of different classes, but part of a related hierarchy.
• Call methods of the most specific common superclass on the array as a whole
• Access properties of the most specific common superclass using dot notation with the array
• Use common operators that are supported for object arrays
• Support array indexing (scalar or nonscalar) that returns arrays of the most specific class
10-19
10 Object Arrays
• Array class
• Property access
• Method invocation
The class of a heterogeneous array is that of the most specific superclass shared by the objects of the
array.
If the following conditions are true, the concatenation and subscripted assignment operations return
a heterogeneous array:
• The objects on the right side of the assignment statement are of different classes
• All objects on the right side of the assignment statement derive from a common subclass of
matlab.mixin.Heterogeneous
For example, form an array by concatenating objects of these classes. The class of a1 is ClassA:
a1 = [SpecificA,SpecificB];
class(a1)
10-20
Designing Heterogeneous Class Hierarchies
ans =
ClassA
If the array includes an object of the class SpecificC, the class of a2 is RootSuperclass:
a2 = [SpecificA,SpecificB,SpecificC];
class(a2)
ans =
RootSuperclass
If you assigned an object of the class SpecificC to array a1 using indexing, the class of a1 becomes
RootSuperclass:
a1(3) = SpecificC;
class(a1)
ans =
RootSuperclass
If the array contains objects of only one class, then the array is not heterogeneous. For example, the
class of a is SpecificA.
a = [SpecificA,SpecificA];
class(a)
ans =
SpecificA
Property Access
Access array properties with dot notation when the class of the array defines the properties. The
class of the array is the most specific common superclass, which ensures all objects inherit the same
properties.
Referring to Prop1 using dot notation returns the value of Prop1 for each object in the array.
Invoking Methods
To invoke a method on a heterogeneous array, the class of the array must define or inherit the method
as Sealed. For example, suppose RootSuperclass defines a Sealed method called superMethod.
Sealing the method (so that it cannot be overridden in a subclass) ensures that the same method
definition exists for all elements of the array. Calling that method on a single element of the array
invokes the same method implementation as calling the method on the whole array.
10-21
10 Object Arrays
Unsupported Hierarchies
Heterogeneous hierarchies cannot have ambiguities when obtaining default objects, determining the
class of the array, and converting class objects to other types. Members of the hierarchy can derive
from only one root superclass (that is, from only one direct subclass of
matlab.mixin.Heterogeneous).
The next diagram shows two separate heterogeneous hierarchies. ClassA has only one root
superclass (called OtherBaseClass). The heterogeneous hierarchy is no longer ambiguous:
10-22
Designing Heterogeneous Class Hierarchies
Default Object
A default object is the object returned by calling the class constructor with no arguments. MATLAB
uses default objects in these situations:
• Indexed assignment creates an array with gaps in array elements. For example, assign the first
element of array h to index 5:
h(5) = ClassA(arg1,arg2);
Heterogeneous hierarchies enable you to define the default object for that hierarchy. The
matlab.mixin.Heterogeneous class provides a default implementation of a method called
getDefaultScalarElement. This method returns an instance of the root class of the
heterogeneous hierarchy, unless the root superclass is abstract.
If the root superclass is abstract or is not appropriate for a default object, override the
getDefaultScalarElement method. Implement the getDefaultScalarElement override in the
root superclass, which derives directly from matlab.mixin.Heterogeneous.
getDefaultScalarElement must return a scalar object that is derived from the root superclass.
For specific information on how to implement this method, see
matlab.mixin.Heterogeneous.getDefaultScalarElement.
10-23
10 Object Arrays
To support the formation of heterogeneous arrays using objects that are not part of the
heterogeneous hierarchy, implement a convertObject method in the root superclass. The
convertObject method must convert the nonmember object to a valid member of the
heterogeneous hierarchy.
For example, suppose RootSuperclass is an abstract class that is the root of a heterogeneous
hierarchy. Initialize an array using the empty static method:
ary = RootSuperclass.empty;
See Also
Related Examples
• “A Class Hierarchy for Heterogeneous Arrays” on page 20-2
• “Handle-Compatible Classes and Heterogeneous Arrays” on page 12-38
10-24
Heterogeneous Array Constructors
When assigning to object arrays, MATLAB uses the default object to fill in unassigned array elements.
In a heterogeneous hierarchy, the default object can be the superclass that is called by the subclass
constructor. Therefore, building an array in the superclass constructor can create a heterogeneous
array.
10-25
10 Object Arrays
In this code, the superclass constructor creates one object for each element in the input argument,
arg:
methods
function obj = SuperClass(arg)
...
n = numel(arg);
obj = repelem(obj,1,n);
for k = 1:n
obj(k).SuperProp = arg(k);
end
...
end
end
The subclass constructor calls the superclass constructor to pass the required argument array, a:
methods
function obj = SubClass(a)
obj = obj@SuperClass(a);
for k = 1:numel(a)
obj(k).SubProp = a(k);
end
end
end
Sample Implementation
The following class hierarchy defines a subclass that builds object arrays in its constructor. The root
superclass of the hierarchy initializes the superclass part of the objects in the array.
This class hierarchy represents members of an engineering team. The classes in the hierarchy
include:
10-26
Heterogeneous Array Constructors
The TeamMembers class is the root of the heterogeneous hierarchy and is a concrete class. Before
assigning values to the Name and PhoneX properties, the constructor initializes an array of subclass
(ProjectEngineer) objects.
The ProjectEngineer constructor provides the obj argument for the call to repelem with this
statement:
obj = obj@TeamMembers(varargin{1:2});
10-27
10 Object Arrays
The ProjectEngineer class represents one type of team member. This class supports array inputs
and returns an array of objects.
The ProjectEngineer class requires a cell array of names, a numeric array of phone extensions,
and a cell array of billing rates for each engineer in the team.
nm = {'Fred','Nancy','Claudette'};
px = [8112,8113,8114];
rt = {'C2','B1','A2'};
tm = ProjectEngineer(nm,px,rt)
tm =
Rate
Name
PhoneX
Potential Error
The TeamMembers constructor initializes the object array with this statement:
obj = repelem(obj,1,n);
Because the obj argument to repelem is a ProjectEngineer object, the array returned is of the
same class.
Without this statement, the TeamMembers constructor would create default objects to fill in array
elements in the for loop. The resulting heterogeneous array would be of the class of the common
superclass (TeamMembers in this case). If the superclass returns this heterogeneous array to the
subclass constructor, it is a violation of the rule that class constructors must preserve the class of the
returned object.
10-28
Heterogeneous Array Constructors
See Also
More About
• “Designing Heterogeneous Class Hierarchies” on page 10-18
10-29
11
For more information, see “Event and Listener Concepts” on page 11-12.
Predefined Events
MATLAB defines events for listening to property sets and queries. For more information, see “Listen
for Changes to Property Values” on page 11-32.
All handle objects define an event named ObjectBeingDestroyed. MATLAB triggers this event
before calling the class destructor.
Event Syntax
Define an event name in the events code block:
11-2
Overview Events and Listeners
methods
function anyMethod(obj)
notify(obj,'EventName');
end
end
end
Any function or method can trigger the event for a specific instance of the class defining the event.
For example, the triggerEvent method calls notify to trigger the StateChange event:
obj = MyClass;
obj.triggerEvent
For more information, see “Events and Listeners Syntax” on page 11-18.
Create Listener
Define a listener using the handle class addlistener or listener method. Pass a function handle
for the listener callback function using one of these syntaxes:
11-3
11 Events — Sending and Responding to Messages
• SourceOfEvent — An object of the class that defines the event. The event is triggered on this
object.
• EventName — The name of the event defined in the class events code block.
• @listenerCallback — a function handle referencing the function that executes in response to
the event.
function lh = createListener(src)
lh = addlistener(src,'StateChange',@handleStateChange)
end
Define the callback function for the listener. The callback function must accept as the first two
arguments the event source object and an event data object: Use the event source argument to
access the object that triggered the event. Find information about the event using the event data
object.
function handleStateChange(src,eventData)
% src - handle to object that triggered the event
% eventData - event.EventData object containing
% information about the event.
...
end
See Also
event.EventData | handle
Related Examples
• “Listener Lifecycle” on page 11-23
• “Implement Property Set Listener” on page 11-10
11-4
Define Custom Event Data
Events provide information to listener callback functions by passing an event data argument to the
specified function. By default, MATLAB passes an event.EventData object to the listener callback.
This object has two properties:
Provide additional information to the listener callback by subclassing the event.EventData class.
11-5
11 Events — Sending and Responding to Messages
obj.Prop1 = value;
if (obj.Prop1 > 10)
% Trigger the event using custom event data
notify(obj,'Overflow',SpecialEventDataClass(orgvalue));
end
end
end
end
The function setupSEC instantiates the SimpleEventClass class and adds a listener to the object.
In this example, the listener callback function displays information that is contained in the
eventData argument (which is a SpecialEventDataClass object).
sec = setupSEC;
sec.Prop1 = 5;
sec.Prop1 = 15; % listener triggers callback
11-6
Define Custom Event Data
See Also
Related Examples
• “Observe Changes to Property Values” on page 11-8
11-7
11 Events — Sending and Responding to Messages
The PropLis class uses an ordinary method (attachListener) to add the listener for the
ObservedProp property. If the PropLis class defines a constructor, the constructor can contain the
call to addlistener.
The listener callback is a static method (propChange). MATLAB passes two arguments when calling
this function:
These arguments provide information about the property and the event.
Use the PropLis class by creating an instance and calling its attachListener method:
plObj = PropLis;
plObj.ObservedProp
ans =
plObj.attachListener
plObj.ObservedProp = 2;
11-8
Observe Changes to Property Values
See Also
event.proplistener | addlistener | listener
Related Examples
• “Listener Lifecycle” on page 11-23
• “Implement Property Set Listener” on page 11-10
11-9
11 Events — Sending and Responding to Messages
The push button's callback is a class method (named pressed). When the push button is activated,
the following sequence occurs:
1 MATLAB executes the pressed method, which graphs a new set of data and increments the
ResultNumber property.
2 Attempting to set the value of the ResultNumber property triggers the PreSet event, which
executes the listener callback before setting the property value.
3 The listener callback uses the event data to obtain the handle of the callback object (an instance
of the PushButton class), which then provides the handle of the axes object that is stored in its
AxHandle property.
4 The listener callback updates the axes Title property, after the callback completes execution,
MATLAB sets the ResultsNumber property to its new value.
11-10
Implement Property Set Listener
scatter(obj.AxHandle,randn(1,20),randn(1,20),'p')
obj.ResultNumber = obj.ResultNumber + 1;
end
end
methods (Static)
function updateTitle(~,eventData)
h = eventData.AffectedObject;
set(get(h.AxHandle,'Title'),'String',['Result Number: ',...
num2str(h.ResultNumber)])
end
end
end
The scatter graph looks similar to this graph after three push-button clicks.
buttonObj = PushButton;
See Also
Related Examples
• “Listen for Changes to Property Values” on page 11-32
11-11
11 Events — Sending and Responding to Messages
Basically, any activity that you can detect programmatically can generate an event and communicate
information to other objects.
MATLAB classes define a process that communicates the occurrence of events to other objects that
respond to the events. The event model works this way:
• A handle class declares a name used to represent an event. “Name Events” on page 11-18
• After creating an object of the event-declaring class, attach listener to that object. “Control
Listener Lifecycle” on page 11-23
• A call to the handle class notify method broadcasts a notice of the event to listeners. The class
user determines when to trigger the event. “Trigger Events” on page 11-18
• Listeners execute a callback function when notified that the event has occurred. “Specifying
Listener Callbacks” on page 11-24
• You can bind listeners to the lifecycle of the object that defines the event, or limit listeners to the
existence and scope of the listener object. “Control Listener Lifecycle” on page 11-23
11-12
Event and Listener Concepts
Limitations
There are certain limitations to the use of events:
• The event source cannot guarantee that listeners exist when triggering the event.
• A listener cannot prevent other listeners from being notified that the event occurred.
• The order in which listeners execute is not defined.
• Listeners should not modify the event data object passed to the listener callback, because other
listeners are passed this same handle object.
11-13
11 Events — Sending and Responding to Messages
MATLAB passes the source object to the listener callback in the required event data argument. Use
the source object to access any of the object's public properties from within your listener callback
function.
You can create a subclass of the event.EventData class to provide additional information to
listener callback functions. The subclass would define properties to contain the additional data and
provide a method to construct the derived event data object so it can be passed to the notify
method.
“Define Event-Specific Data” on page 11-21 provides an example showing how to customize this
data.
“Comparison of Handle and Value Classes” on page 7-2 provides general information on handle
classes.
“Events and Listeners Syntax” on page 11-18 shows the syntax for defining a handle class and
events.
• PreSet — Triggered just before the property value is set, before calling its set access method
• PostSet — Triggered just after the property value is set
• PreGet — Triggered just before a property value query is serviced, before calling its get access
method
• PostGet — Triggered just after returning the property value to the query
These events are predefined and do not need to be listed in the class events block.
When a property event occurs, the callback is passed an event.PropertyEvent object. This object
has three properties:
You can define your own property-change event data by subclassing the event.EventData class.
The event.PropertyEvent class is a sealed subclass of event.EventData.
11-14
Event and Listener Concepts
See “Listen for Changes to Property Values” on page 11-32 for a description of the process for
creating property listeners.
See “Property Get and Set Methods” on page 8-38 for information on methods that control access to
property values.
Listeners
Listeners encapsulate the response to an event. Listener objects belong to the event.listener
class, which is a handle class that defines the following properties:
• Source — Handle or array of handles of the object that generated the event
• EventName — Name of the event
• Callback — Function to execute when an enabled listener receives event notification
• Enabled — Callback function executes only when Enabled is true. See “Enable and Disable
Listeners” on page 11-50 for an example.
• Recursive — Allow listener to trigger the same event that caused execution of the callback.
Recursive is false by default. If the callback triggers the event for which it is defined as the
callback, the listener cannot execute recursively. Therefore, set Recursive to true if the
callback must trigger its own event. Setting the Recursive property to true can create a
situation where infinite recursion reaches the recursion limit and triggers an error.
11-15
11 Events — Sending and Responding to Messages
Event Attributes
To define other events in the same class definition that have different attribute settings, create
another events block.
11-16
Event Attributes
Event Attributes
See Also
Related Examples
• “Events and Listeners Syntax” on page 11-18
11-17
11 Events — Sending and Responding to Messages
Components to Implement
Implementation of events and listeners involves these components:
• Specification of the name of an event in a handle class — “Name Events” on page 11-18.
• A function or method to trigger the event when the action occurs — “Trigger Events” on page 11-
18.
• Listener objects to execute callback functions in response to the triggered event — “Listen to
Events” on page 11-19.
• Default or custom event data that the event passes to the callback functions — “Define Event-
Specific Data” on page 11-21.
Name Events
Define an event by declaring an event name inside an events block. For example, this class creates
an event called ToggleState:
Events cannot have the same name as the class that defines them.
Trigger Events
The OnStateChange method calls notify to trigger the ToggleState event. Pass the handle of the
object that is the source of the event and the name of the event to notify.
11-18
Events and Listeners Syntax
function OnStateChange(obj,newState)
if newState ~= obj.State
obj.State = newState;
notify(obj,'ToggleState');
end
end
end
end
Listen to Events
After the call to notify triggers an event, MATLAB broadcasts a message to all listeners that are
defined for that event and source object. There are two ways to create listeners: using the handle
class addlistener or listener method.
If you want the listener to persist beyond the normal variable scope, use addlistener to create it.
The event source object holds a reference to the listener object. When the event source object is
destroyed, MATLAB destroys the listener.
lh = addlistener(obj,'ToggleState',@RespondToToggle.handleEvnt);
Use the listener method to create listeners when you want to manage the lifecycle of the listener
and do not want a coupling between the event source and listener object. MATLAB does not destroy
listeners created with listener when the event source is destroyed. However, your code must keep
the listener object handle in scope when creating listeners using listener.
The listener method requires the same arguments as addlistener: the event-naming object, the
event name, and a function handle to the callback. listener returns the handle to the listener
object.
lh = listener(obj,'EventName',@callbackFunction)
For example, this code uses the ToggleState event discussed previously:
lh = listener(obj,'ToggleState',@RespondToToggle.handleEvnt)
Callback Function
The listener callback function must accept a minimum of two arguments, which MATLAB
automatically passes to the callback. Here are the required arguments:
• The source of the event — that is, obj in the call to addlistener or event.listener.
11-19
11 Events — Sending and Responding to Messages
Define the callback function to accept the source object and event data arguments.
function callbackFunction(src,evtdata)
...
end
For more information on callback syntax, see “Listener Callback Syntax” on page 11-24.
Define Listener
The RespondToToggle class defines objects that listen for the ToggleState event defined in the
ToggleButton class.
classdef RespondToToggle < handle
methods
function obj = RespondToToggle(toggle_button_obj)
addlistener(toggle_button_obj,'ToggleState',@RespondToToggle.handleEvnt);
end
end
methods (Static)
function handleEvnt(src,~)
if src.State
disp('ToggleState is true')
else
disp('ToggleState is false')
end
end
end
end
The class RespondToToggle adds the listener in its constructor. In this case, the class defines the
callback (handleEvnt) as a static method that accepts the two required arguments:
• src — The handle of the object triggering the event (that is, a ToggleButton object)
• evtdata — An event.EventData object
tb = ToggleButton;
rtt = RespondToToggle(tb);
Whenever you call the OnStateChange method of the ToggleButton object, notify triggers the
event. For this example, the callback displays the value of the State property:
tb.OnStateChange(true)
ToggleState is true
tb.OnStateChange(false)
ToggleState is false
Remove Listeners
Remove a listener object by calling delete on its handle. For example, if the class
RespondToToggle saved the listener handle as a property, you could delete the listener.
classdef RespondToToggle < handle
properties
11-20
Events and Listeners Syntax
With this code change, you can remove the listener from an instance of the RespondToToggle class.
For example:
tb = ToggleButton;
rtt = RespondToToggle(tb);
The object rtt is listening for the ToggleState event triggered by object tb. To remove the listener,
call delete on the property containing the listener handle.
delete(rtt.ListenerHandle)
Note To save and load objects that are subclasses of event.EventData, such as
ToggleEventData, enable the ConstructOnLoad class attribute for the subclass.
methods
function data = ToggleEventData(newState)
data.NewState = newState;
end
end
end
The call to notify can use the ToggleEventData constructor to create the necessary argument.
evtdata = ToggleEventData(newState);
notify(obj,'ToggleState',evtdata);
11-21
11 Events — Sending and Responding to Messages
See Also
Related Examples
• “Listener Callback Syntax” on page 11-24
• “Listen for Changes to Property Values” on page 11-32
• “Techniques for Using Events and Listeners” on page 11-40
11-22
Listener Lifecycle
Listener Lifecycle
In this section...
“Control Listener Lifecycle” on page 11-23
“Temporarily Deactivate Listeners” on page 11-23
“Permanently Delete Listeners” on page 11-23
• addlistener creates a coupling between the listener and event source object. The listener object
persists until you delete it or until the event object is destroyed. When the event source object is
destroyed, MATLAB automatically destroys the listener object.
• listener constructs listener objects that are not coupled to the lifecycle of the event source
object. The listener is active as long as the listener object remains in scope and is not explicitly
deleted. Therefore, your application must maintain a reference to the listener object by storing the
listener handle. The advantage of uncoupling the listener and event objects is that you can define
and destroy each independently.
For more information, see “Events and Listeners Syntax” on page 11-18.
ListenerHandle.Enabled = false;
ListenerHandle.Enabled = true;
delete(ListenerHandle)
See Also
Related Examples
• “Enable and Disable Listeners” on page 11-50
11-23
11 Events — Sending and Responding to Messages
If you do not use the event source and event data arguments, you can define the function to ignore
these inputs:
function callbackFunction(~,~)
...
end
For a method:
function callbackMethod(obj,src,evnt)
...
end
11-24
Listener Callback Syntax
Here is the syntax for an ordinary method. The input arguments (arg1,...argn) must be defined in
the context in which you call addlistener.
lh = addlistener(src,'EventName',@(src,evnt)obj.callbackMethod(src,evnt,arg1,...argn)
The TestAnonyFcn class shows the use of an anonymous function with an additional argument. The
listener callback displays the inputs arguments to show how MATLAB calls the callback method.
classdef TestAnonyFcn < handle
events
Update
end
methods
function obj = TestAnonyFcn
t = datestr(now);
addlistener(obj,'Update',@(src,evnt)obj.evntCb(src,evnt,t));
end
function triggerEvnt(obj)
notify(obj,'Update')
end
end
methods (Access = private)
function evntCb(~,~,evnt,varargin)
disp(['Number of inputs: ',num2str(nargin)])
disp(evnt.EventName)
disp(varargin{:})
end
end
end
Create an object and trigger the event by calling the triggerEvt method:
obj = TestAnonyFcn;
obj.triggerEvnt;
Number of inputs: 4
Update
01-Jul-2008 17:19:36
11-25
11 Events — Sending and Responding to Messages
See Also
Related Examples
• “Callback Execution” on page 11-27
• “Create Function Handle”
11-26
Callback Execution
Callback Execution
In this section...
“When Callbacks Execute” on page 11-27
“Listener Order of Execution” on page 11-27
“Callbacks That Call notify” on page 11-27
“Manage Callback Errors” on page 11-27
“Invoke Functions from Function Handles” on page 11-27
Callback function execution continues until the function completes. If an error occurs in a callback
function, execution stops and control returns to the calling function. Then any remaining listener
callback functions execute.
The handle class notify method calls all listeners before returning execution to the function that
called notify.
Listener callbacks can call notify to trigger events, including the same event that invoked the
callback. When a function calls notify, MATLAB sets the property values of the event data object
that is passed to callback functions. To ensure that these properties have appropriate values for
subsequently called callbacks, always create a new event data object if you call notify with custom
event data.
11-27
11 Events — Sending and Responding to Messages
For example, the UpdateEvt class defines an event named Update and a listener for that event. The
listener callback is the private method evtCb.
Private methods are normally accessible only by class methods. However, because the function
handle is created in a class method, notify can execute the callback from outside of the class:
a = UpdateEvt;
a.notify('Update')
See Also
Related Examples
• “Listener Callback Syntax” on page 11-24
11-28
Determine If Event Has Listeners
Note When called, event.hasListener must have NotifyAccess for the event. That is, call
event.hasListener in a context in which you can call notify for the event in question.
Coding Patterns
• Conditionalize the creation of event data and the call to notify using event.hasListener. For
an object array a, determine if there are listeners before creating event data and triggering the
event:
if any(event.hasListener(a,'NameOfEvent'))
evt = MyCustomEventData(...);
notify(a,'NameOfEvent',evt)
end
• Trigger events selectively using logical indexing with the values returned by
event.hasListener. Send event notifications only for array elements that have listeners:
ind = event.hasListeners(a,'NameOfEvent');
notify(a(ind),'NameOfEvent',evt)
For example, in the following diagram, the class of a heterogeneous array formed with objects of
classes SpecificA, SpecificB, and SpecificC is RootSuperclass. Therefore,
event.hasListener can find listeners only for the RootEvent event, because it is the only event
common to all array elements.
11-29
11 Events — Sending and Responding to Messages
het = [SpecificA,SpecificB,SpecificC];
class(het)
ans
RootSuperclass
events(het)
RootEvent
event.hasListener cannot determine if there are listeners for events that are defined by some but
not all objects in the array:
event.hasListener(het,'ClassAEvent')
Determine if individual objects in the heterogeneous array have listeners defined for their specific
events, by indexing into the array:
event.hasListener(het(1),'ClassAEvent')
For more information about determining the class of heterogeneous arrays, see “Designing
Heterogeneous Class Hierarchies” on page 10-18.
11-30
Determine If Event Has Listeners
See Also
Related Examples
• “Listener Lifecycle” on page 11-23
11-31
11 Events — Sending and Responding to Messages
For handle classes, you can define listeners for the predeclared property events (named: PreSet,
PostSet, PreGet, and PostGet). To create listeners for those named events:
In the properties block, enable the SetObservable attribute. You can define PreSet and PostSet
listeners for the properties defined in this block:
properties (SetObservable)
PropOne
PropTwo
end
The listener executes the callback function when MATLAB triggers the property event. Define the
callback function to have two specific arguments, which are passed to the function automatically
when called by the listener:
• Event source — a meta.property object describing the object that is the source of the property
event
• Event data — a event.PropertyEvent object containing information about the event
You can pass additional arguments if necessary. It is often simple to define this method as Static
because these two arguments contain most necessary information in their properties.
For example, suppose the handlePropEvents function is a static method of the class creating
listeners for two properties of an object of another class:
methods (Static)
function handlePropEvents(src,evnt)
switch src.Name
case 'PropOne'
% PropOne has triggered an event
case 'PropTwo'
11-32
Listen for Changes to Property Values
Another possibility is to use the event.PropertyEvent object's EventName property in the switch
statement to key off the event name (PreSet or PostSet in this case).
“Class Metadata” on page 16-2 provides more information about the meta.property class.
The addlistener handle class method enables you to attach a listener to a property without storing
the listener object as a persistent variable. For a property event, use the four-argument version of
addlistener.
If your listener callback is an ordinary method and not a static method, the syntax is:
addlistener(EventObject,'PropOne','PostSet',@obj.handlePropertyEvents);
where obj is the handle of the object defining the callback method.
If the listener callback is a function that is not a class method, you pass a function handle to that
function. Suppose that the callback function is a package function:
addlistener(EventObject,'PropOne','PostSet',@package.handlePropertyEvents);
For more information on passing functions as arguments, see “Create Function Handle”.
The PropEvent class enables property PreSet and PostSet event triggering by specifying the
SetObservable property attribute. These properties also enable the AbortSet attribute, which
prevents the triggering of the property events if the properties are set to a value that is the same as
their current value (see “Assignment When Property Value Is Unchanged” on page 11-35).
11-33
11 Events — Sending and Responding to Messages
end
methods
function obj = PropEvent(p1,p2)
if nargin > 0
obj.PropOne = p1;
obj.PropTwo = p2;
end
end
end
end
You can define listeners for other events or other properties using a similar approach. It is not
necessary to use the same callback function for each listener. See the meta.property and
event.PropertyEvent reference pages for more on the information contained in the arguments
passed to the listener callback function.
classdef PropListener < handle
% Define property listeners
methods
function obj = PropListener(evtobj)
if nargin > 0
addlistener(evtobj,'PropOne','PostSet',@PropListener.handlePropEvents);
addlistener(evtobj,'PropTwo','PostSet',@PropListener.handlePropEvents);
end
end
end
methods (Static)
function handlePropEvents(src,evnt)
switch src.Name
case 'PropOne'
sprintf('PropOne is %s\n',num2str(evnt.AffectedObject.PropOne))
case 'PropTwo'
sprintf('PropTwo is %s\n',num2str(evnt.AffectedObject.PropTwo))
end
end
end
end
See Also
Related Examples
• “Assignment When Property Value Is Unchanged” on page 11-35
11-34
Assignment When Property Value Is Unchanged
You can prevent these actions by setting the property's AbortSet attribute to true. When AbortSet
is enabled, MATLAB compares the current property value to the new value being assigned to the
property. If the new value is the same as the current value, MATLAB does not:
To compare values, MATLAB must get the current value of the property. Getting the current value
causes the property get method (get.Property) to execute, if one exists. Any errors that occur
when calling the property get method are visible to the user, even if MATLAB does not change the
current value.
Note The AbortSet attribute only works with properties defined in handle classes. It cannot be
used with value classes.
• You want to prevent notification of the PreSet and PostSet events and execution of the listener
callbacks when the property value does not change.
11-35
11 Events — Sending and Responding to Messages
• The cost of setting a property value is greater than the cost of comparing the current property
value with the value being assigned, and you are willing to incur the comparison cost with all
assignments to the property.
Implement AbortSet
The following example shows how the AbortSet attribute works. The AbortTheSet class defines a
property, PropOne, that has listeners for the PreGet, PreSet, PostGet, and PostSet events and
enables the AbortSet attribute.
Note To use this class, save the AbortTheSet class in a file with the same name in a folder on your
MATLAB path.
11-36
Assignment When Property Value Is Unchanged
The class specifies an initial value of 7 for the PropOne property. Therefore, if you create an object
and assign the property value of 7, there is no need to trigger the PreSet event. However, the
getPropOne method is called to get the current value of the property to compare to the assigned
vale.
obj = AbortTheSet;
obj.PropOne = 7;
get.PropOne called
If you specify a value other than 7, then MATLAB performs these steps:
obj = AbortTheSet;
obj.PropOne = 9;
get.PropOne called
Pre-set event triggered
set.PropOne called
Post-set event triggered
If you query the property value, the PreGet and PostGet events are triggered.
obj.PropOne
ans =
11-37
11 Events — Sending and Responding to Messages
end
function propval = get.PropOne(obj)
disp('get.PropOne called')
propval = obj.PropOne;
end
function set.PropOne(obj,val)
disp('set.PropOne called')
obj.PropOne = val;
end
function getPrePropEvt(obj,src,evnt)
disp ('Pre-get event triggered')
% ...
end
function setPrePropEvt(obj,src,evnt)
disp ('Pre-set event triggered')
% ...
end
function getPostPropEvt(obj,src,evnt)
disp ('Post-get event triggered')
% ...
end
function setPostPropEvt(obj,src,evnt)
disp ('Post-set event triggered')
% ...
end
function disp(obj)
% Overload disp to avoid accessing property
disp (class(obj))
end
end
end
Because MATLAB applies scalar expansion to satisfy the size restriction, the following assignment
does not trigger the PreSet or PostSet events.
obj = AbortTheSet;
obj.PropOne = 7;
get.PropOne called
obj.PropOne
ans =
7 7 7
For information on property validation, see “Validate Property Values” on page 8-18.
See Also
Related Examples
• “Property Get and Set Methods” on page 8-38
11-38
Assignment When Property Value Is Unchanged
11-39
11 Events — Sending and Responding to Messages
Example Overview
This example defines two classes:
• fcneval — The function evaluator class contains a MATLAB expression and evaluates this
expression over a specified range
• fcnview — The function viewer class contains a fcneval object and displays surface graphs of
the evaluated expression using the data contained in fcneval.
• A class-defined event that occurs when a new value is specified for the MATLAB function
• A property event that occurs when the property containing the limits is changed
The following diagram shows the relationship between the two objects. The fcnview object contains
a fcneval object and creates graphs from the data it contains. fcnview creates listeners to change
the graphs if any of the data in the fcneval object change.
11-40
Techniques for Using Events and Listeners
11-41
11 Events — Sending and Responding to Messages
Method Purpose
fcneval Class constructor. Inputs are function handle and two-element vector
specifying the limits over which to evaluate the function.
set.FofXY FofXY property set function. Called whenever property value is set, including
during object construction.
set.Lm Lm property set function. Used to test for valid limits.
get.Data Data property get function. This method calculates the values for the Data
property whenever that data is queried (by class members or externally).
grid A static method (Static attribute set to true) used in the calculation of the
data.
Method Purpose
fcnview Class constructor. Input is fcneval object.
createLisn Calls addlistener to create listeners for UpdateGraph and Lm
property PostSet listeners.
11-42
Techniques for Using Events and Listeners
Method Purpose
lims Sets axes limits to current value of fcneval object's Lm property.
Used by event handlers.
updateSurfaceData Updates the surface data without creating a new object. Used by
event handlers.
listenUpdateGraph Callback for UpdateGraph event.
listenLm Callback for Lm property PostSet event
delete Delete method for fcnview class.
createViews Static method that creates an instance of the fcnview class for each
subplot, defines the context menus that enable/disable listeners, and
creates the subplots
“Handle Class Methods” on page 7-11 provides a complete list of methods that are inherited when
you subclass the handle class.
• Create an instance of the fcneval class to contain the MATLAB expression of a function of two
variables and the range over which you want to evaluate this function
• Use the fcnview class static function createViews to visualize the function
• Change the MATLAB expression or the limits contained by the fcneval object and all the
fcnview objects respond to the events generated.
You create a fcneval object by calling its constructor with two arguments—an anonymous function
and a two-element, monotonically increasing vector. For example:
Use the createViews static method to create the graphs of the function. Use the class name to call
a static function:
fcnview.createViews(feobject);
The createView method generates four views of the function contained in the fcneval object.
11-43
11 Events — Sending and Responding to Messages
Each subplot defines a context menu that can enable and disable the listeners associated with that
graph. For example, if you disable the listeners on subplot 221 (upper left) and change the MATLAB
expression contained by the fcneval object, only the remaining three subplots update when the
UpdateGraph event is triggered:
Similarly, if you change the limits by assigning a value to the feobject.Lm property, the feobject
triggers a PostSet property event and the listener callbacks update the graph.
11-44
Techniques for Using Events and Listeners
In this figure, the listeners are reenabled via the context menu for subplot 221. Because the listener
callback for the property PostSet event also updates the surface data, all views are now
synchronized
The UpdateGraph event is a class-defined event. The fcneval class names the event and calls
notify when the event occurs.
11-45
11 Events — Sending and Responding to Messages
The fcnview class defines a listener for this event. When fcneval triggers the event, the fcnview
listener executes a callback function that performs the follow actions:
• Determines if the handle of the surface object stored by the fcnview object is still valid (that is,
does the object still exist)
• Updates the surface XData, YData, and ZData by querying the fcneval object's Data property.
events
UpdateGraph
end
The fcneval class defines a property set method for the FofXY property. FofXY is the property that
stores the MATLAB expression for the mathematical function. This expression must be a valid
MATLAB expression for a function of two variables.
11-46
Techniques for Using Events and Listeners
If fcneval.isSuitable does not return an MException object, the set.FofXY method assigns
the value to the property and triggers the UpdateGraph event.
function set.FofXY(obj,func)
% Determine if function is suitable to create a surface
me = fcneval.isSuitable(func);
if ~isempty(me)
throw(me)
end
% Assign property value
obj.FofXY = func;
% Trigger UpdateGraph event
notify(obj,'UpdateGraph');
end
The set.FofXY method calls a static method (fcneval.isSuitable) to determine the suitability of
the specified expression. fcneval.isSuitable returns an MException object if it determines that
the expression is unsuitable. fcneval.isSuitable calls the MException constructor directly to
create more useful error messages for the user.
set.FofXY issues the exception using the throw method. Issuing the exception terminates execution
of set.FofXY and prevents the method from making an assignment to the property or triggering the
UpdateGraph event.
The fcneval.isSuitable method could provide additional test to ensure that the expression
assigned to the FofXY property meets the criteria required by the class design.
11-47
11 Events — Sending and Responding to Messages
Other Approaches
The class could have implemented a property set event for the FofXY property and would, therefore,
not need to call notify (see “Listen for Changes to Property Values” on page 11-32). Defining a class
event provides more flexibility in this case because you can better control event triggering.
For example, suppose that you wanted to update the graph only if the new data is different. If the new
expression produced the same data within some tolerance, the set.FofXY method could not trigger
the event and avoid updating the graph. However, the method could still set the property to the new
value.
The fcnview class creates a listener for the UpdateGraph event using the addlistener method:
obj.HLUpdateGraph = addlistener(obj.FcnObject,'UpdateGraph',...
@(src,evnt)listenUpdateGraph(obj,src,evnt)); % Add obj to argument list
The fcnview object stores a handle to the event.listener object in its HLUpdateGraph property,
which is used to enable/disable the listener by a context menu (see “Enable and Disable Listeners” on
page 11-50).
The fcnview object (obj) is added to the two default arguments (src, evnt) passed to the listener
callback. Keep in mind, the source of the event (src) is the fcneval object, but the fcnview object
contains the handle of the surface object that the callback updates.
function listenUpdateGraph(obj,src,evnt)
if ishandle(obj.HSurface) % If surface exists
obj.updateSurfaceData % Update surface data
end
end
The updateSurfaceData function is a class method that updates the surface data when a different
mathematical function is assigned to the fcneval object. Updating a graphics object data is more
efficient than creating a new object using the new data:
function updateSurfaceData(obj)
% Get data from fcneval object and set surface data
set(obj.HSurface,...
'XData',obj.FcnObject.Data.X,...
'YData',obj.FcnObject.Data.Y,...
'ZData',obj.FcnObject.Data.Matrix);
end
11-48
Techniques for Using Events and Listeners
The fcneval class defines a set function for the Lm property. When a value is assigned to this
property during object construction or property reassignment, the following sequence occurs:
The PostSet event does not occur until an actual assignment of the property occurs. The property
set function provides an opportunity to deal with potential assignment errors before the PostSet
event occurs.
To create a listener for the PostSet event, you must set the property's SetObservable attribute to
true:
11-49
11 Events — Sending and Responding to Messages
The fcnview class creates a listener for the PostSet event using the addlistener method:
obj.HLLm = addlistener(obj.FcnObject,'Lm','PostSet',...
@(src,evnt)listenLm(obj,src,evnt)); % Add obj to argument list
The fcnview object stores a handle to the event.listener object in its HLLm property, which is
used to enable/disable the listener by a context menu (see “Enable and Disable Listeners” on page
11-50).
The fcnview object (obj) is added to the two default arguments (src, evnt) passed to the listener
callback. Keep in mind, the source of the event (src) is the fcneval object, but the fcnview object
contains the handle of the surface object that the callback updates.
The callback sets the axes limits and updates the surface data because changing the limits causes the
mathematical function to be evaluated over a different range:
function listenLm(obj,src,evnt)
if ishandle(obj.HAxes) % If there is an axes
lims(obj); % Update its limits
if ishandle(obj.HSurface) % If there is a surface
obj.updateSurfaceData % Update its data
end
end
end
There are two callbacks used by the context menu corresponding to the two items on the menu:
• Listen — Sets the Enabled property for both the UpdateGraph and PostSet listeners to true
and adds a check mark next to the Listen menu item.
• Don't Listen — Sets the Enabled property for both the UpdateGraph and PostSet listeners to
false and adds a check mark next to the Don't Listen menu item.
Both callbacks include the fcnview object as an argument (in addition to the required source and
event data arguments) to provide access to the handle of the listener objects.
The enableLisn function is called when the user selects Listen from the context menu.
function enableLisn(obj,src,evnt)
obj.HLUpdateGraph.Enabled = true; % Enable listener
obj.HLLm.Enabled = true; % Enable listener
set(obj.HEnableCm,'Checked','on') % Check Listen
11-50
Techniques for Using Events and Listeners
The disableLisn function is called when the user selects Don't Listen from the context menu.
function disableLisn(obj,src,evnt)
obj.HLUpdateGraph.Enabled = false; % Disable listener
obj.HLLm.Enabled = false; % Disable listener
set(obj.HEnableCm,'Checked','off') % Uncheck Listen
set(obj.HDisableCm,'Checked','on') % Check Don't Listen
end
events
UpdateGraph
end
methods
function obj = fcneval(fcn_handle,limits)
if nargin > 0
obj.FofXY = fcn_handle;
obj.Lm = limits;
end
end
function set.FofXY(obj,func)
me = fcneval.isSuitable(func);
if ~isempty(me)
throw(me)
end
obj.FofXY = func;
notify(obj,'UpdateGraph');
end
function set.Lm(obj,lim)
if ~(lim(1) < lim(2))
error('Limits must be monotonically increasing')
else
obj.Lm = lim;
end
end
end
end % methods
11-51
11 Events — Sending and Responding to Messages
end
end
methods
function obj = fcnview(fcnobj)
if nargin > 0
obj.FcnObject = fcnobj;
obj.createLisn;
end
end
function createLisn(obj)
obj.HLUpdateGraph = addlistener(obj.FcnObject,'UpdateGraph',...
@(src,evnt)listenUpdateGraph(obj,src,evnt));
obj.HLLm = addlistener(obj.FcnObject,'Lm','PostSet',...
@(src,evnt)listenLm(obj,src,evnt));
end
function lims(obj)
lmts = obj.FcnObject.Lm;
set(obj.HAxes,'XLim',lmts);
set(obj.HAxes,'Ylim',lmts);
end
11-52
Techniques for Using Events and Listeners
function updateSurfaceData(obj)
data = obj.FcnObject.Data;
set(obj.HSurface,...
'XData',data.X,...
'YData',data.Y,...
'ZData',data.Matrix);
end
function listenUpdateGraph(obj,~,~)
if ishandle(obj.HSurface)
obj.updateSurfaceData
end
end
function listenLm(obj,~,~)
if ishandle(obj.HAxes)
lims(obj);
if ishandle(obj.HSurface)
obj.updateSurfaceData
end
end
end
function delete(obj)
if ishandle(obj.HAxes)
delete(obj.HAxes);
else
return
end
end
end
methods (Static)
createViews(a)
end
end
@fcnview/createViews
function createViews(fcnevalobj)
p = pi; deg = 180/p;
hfig = figure('Visible','off',...
'Toolbar','none');
for k=4:-1:1
fcnviewobj(k) = fcnview(fcnevalobj);
axh = subplot(2,2,k);
fcnviewobj(k).HAxes = axh;
hcm(k) = uicontextmenu;
set(axh,'Parent',hfig,...
'FontSize',8,...
'UIContextMenu',hcm(k))
fcnviewobj(k).HEnableCm = uimenu(hcm(k),...
'Label','Listen',...
'Checked','on',...
'Callback',@(src,evnt)enableLisn(fcnviewobj(k),src,evnt));
fcnviewobj(k).HDisableCm = uimenu(hcm(k),...
'Label','Don''t Listen',...
11-53
11 Events — Sending and Responding to Messages
'Checked','off',...
'Callback',@(src,evnt)disableLisn(fcnviewobj(k),src,evnt));
az = p/k*deg;
view(axh,az,30)
title(axh,['View: ',num2str(az),' 30'])
fcnviewobj(k).lims;
surfLight(fcnviewobj(k),axh)
end
set(hfig,'Visible','on')
end
function surfLight(obj,axh)
obj.HSurface = surface(obj.FcnObject.Data.X,...
obj.FcnObject.Data.Y,...
obj.FcnObject.Data.Matrix,...
'FaceColor',[.8 .8 0],'EdgeColor',[.3 .3 .2],...
'FaceLighting','phong',...
'FaceAlpha',.3,...
'HitTest','off',...
'Parent',axh);
lims(obj)
camlight left; material shiny; grid off
colormap copper
end
function enableLisn(obj,~,~)
obj.HLUpdateGraph.Enabled = true;
obj.HLLm.Enabled = true;
set(obj.HEnableCm,'Checked','on')
set(obj.HDisableCm,'Checked','off')
end
function disableLisn(obj,~,~)
obj.HLUpdateGraph.Enabled = false;
obj.HLLm.Enabled = false;
set(obj.HEnableCm,'Checked','off')
set(obj.HDisableCm,'Checked','on')
end
11-54
12
Classification
Organizing classes into hierarchies facilitates the reuse of code and the reuse of solutions to design
problems that have already been solved. You can think of class hierarchies as sets — supersets
(referred to as superclasses or base classes), and subsets (referred to as subclasses or derived
classes). For example, the following picture shows how you could represent an employee database
with classes.
The root of the hierarchy is the Employees class. It contains data and operations that apply to the
set of all employees. Contained in the set of employees are subsets whose members, while still
employees, are also members of sets that more specifically define the type of employee. Subclasses
like TestEngineer are examples of these subsets.
12-2
Hierarchies of Classes — Concepts
When designing classes, your abstraction contains only those elements that are necessary. For
example, the employee hair color and shoe size certainly characterize the employee, but are probably
not relevant to the design of this employee class. Their sales region is relevant only to some employee
so this characteristic belongs in a subclass.
You can usually describe the relationship between an object of a subclass and an object of its
superclass with a statement like:
This relationship implies that objects belonging to a subclass have the same properties, methods, and
events as the superclass. Subclass objects also have any new features defined by the subclass. Test
this relationship with the isa function.
You can pass a subclass object to a superclass method, but you can access only those properties that
the superclass defines. This behavior enables you to modify the subclasses without affecting the
superclass.
Two points about super and subclass behavior to keep in mind are:
12-3
12 How to Build on Other Classes
Therefore, you can treat an Engineer object like any other Employees object, but an Employee
object cannot pass for an Engineer object.
MATLAB determines the class of an object based on its most specific class. Therefore, an Engineer
object is of class Engineer, while it is also an Employees object, as using the isa function reveals.
Generally, MATLAB does not allow you to create arrays containing a mix of superclass and subclass
objects because an array can be of only one class. If you attempt to concatenate objects of different
classes, MATLAB looks for a converter method defined by the less dominant class
See “Concatenating Objects of Different Classes” on page 10-13 for more information.
See “Object Converters” on page 17-5 for information on defining converter methods.
Implementation inheritance enables code reuse by subclasses. For example, an employee class can
have a submitStatus method that all employee subclasses can use. Subclasses can extend an
inherited method to provide specialized functionality, while reusing the common aspects. See “Modify
Inherited Methods” on page 12-13 for more information on this process.
Create an interface using an abstract class as the superclass. This class defines the methods and
properties that you must implement in the subclasses, but does not provide an implementation.
The subclasses must provide their own implementation of the abstract members of the superclass. To
create an interface, define methods and properties as abstract using their Abstract attribute. See
“Abstract Classes and Class Members” on page 12-68 for more information and an example.
See Also
Related Examples
• “Design Subclass Constructors” on page 12-7
12-4
Subclass Syntax
Subclass Syntax
In this section...
“Subclass Definition Syntax” on page 12-5
“Subclass double” on page 12-5
When inheriting from multiple classes, use the & character to indicate the combination of the
superclasses:
See “Class Member Compatibility” on page 12-19 for more information on deriving from multiple
superclasses.
Class Attributes
Subclass double
Suppose you want to define a class that derives from double and restricts values to be positive
numbers. The PositiveDouble class:
• Supports a default constructor (no input arguments). See “No Input Argument Constructor
Requirement” on page 9-18
• Restricts the inputs to positive values using mustBePositive
• Calls the superclass constructor with the input value to create the double numeric value
a = PositiveDouble(1:5);
You can perform operations on objects of this class like any double.
12-5
12 How to Build on Other Classes
sum(a)
ans =
15
a = PositiveDouble(0:5);
See Also
Related Examples
• “Design Subclass Constructors” on page 12-7
• “Subclasses of MATLAB Built-In Types” on page 12-40
12-6
Design Subclass Constructors
If you do not explicitly call the superclass constructors from the subclass constructor, MATLAB
implicitly calls these constructors with no arguments. The superclass constructors must support the
no argument syntax to support implicit calls, and the constructors are called in the order they appear
at the top of the class block, from left to right. To change the order in which MATLAB calls the
constructors or to call constructors with arguments, call the superclass constructors explicitly from
the subclass constructor.
If you do not define a subclass constructor, you can call the default constructor with superclass
arguments. For more information, see “Default Constructor” on page 9-17 and “Implicit Call to
Inherited Constructor” on page 9-21.
obj@SuperClass1(args,...);
...
obj@SuperclassN(args,...);
Where obj is the output of the subclass constructor, SuperClass... is the name of a superclass,
and args are any arguments required by the respective superclass constructor.
For example, the following segment of a class definition shows that a class called Stocks that is a
subclass of a class called Assets.
12-7
12 How to Build on Other Classes
end
end
end
“Subclass Constructors” on page 9-18 provides more information on creating subclass constructor
methods.
If a superclass is contained in a package, include the package name. For example, the Assests class
is in the finance package:
classdef Stocks < finance.Assets
methods
function s = Stocks(asset_args,...)
if nargin == 0
...
end
% Call asset constructor
s@finance.Assets(asset_args);
...
end
end
end
To derive a class from multiple superclasses, initialize the subclass object with calls to each
superclass constructor:
classdef Stocks < finance.Assets & Taxable
methods
function s = Stocks(asset_args,tax_args,...)
if nargin == 0
...
end
% Call asset and member class constructors
s@finance.Assets(asset_args)
s@Taxable(tax_args)
...
end
end
end
For example, the Stocks class constructor supports the no argument case with the if statement, but
calls the superclass constructor outside of the if code block.
classdef Stocks < finance.Assets
properties
NumShares
12-8
Design Subclass Constructors
Symbol
end
methods
function s = Stocks(description,numshares,symbol)
if nargin == 0
description = '';
numshares = 0;
symbol = '';
end
s@finance.Assets(description);
s.NumShares = numshares;
s.Symbol = symbol;
end
end
end
The following implementations of classes A, B, and C show how to design this relationship in each
class.
Class B inherits properties x and y from class A. The class B constructor calls the class A constructor
to initialize x and then assigns a value to y.
classdef B < A
methods
function obj = B(x,y)
...
obj@A(x);
obj.y = y;
end
end
end
Class C accepts values for the properties x and y, and passes these values to the class B constructor,
which in turn calls the class A constructor:
classdef C < B
methods
12-9
12 How to Build on Other Classes
See Also
Related Examples
• “No Input Argument Constructor Requirement” on page 9-18
12-10
Control Sequence of Constructor Calls
If you explicitly call a superclass constructor from the most specific subclass constructor (ClassC in
the following diagram), then MATLAB calls the most specific subclass constructor first. If you do not
make an explicit call to a superclass constructor from the subclass constructor, MATLAB makes the
implicit call when accessing the object.
Suppose that you have a hierarchy of classes in which ClassC derives from ClassB, which derives
from ClassA. The constructor for a subclass can call only direct superclasses. Therefore, each class
constructor can call the direct superclass constructor:
In cases of multiple inheritance, the subclass constructor can call each superclass constructor. To
ensure that a specific superclass constructor calling sequence is followed, call all direct superclass
constructors explicitly from the most specific subclass constructor:
12-11
12 How to Build on Other Classes
See Also
Related Examples
• “Call Only Direct Superclass from Constructor” on page 12-9
• “Class Constructor Methods” on page 9-15
12-12
Modify Inherited Methods
Subclasses override inherited methods (that is, implement a method of the same name) when there is
a need to provide specialized behavior in the subclass. Here are some patterns that override
superclass methods.
• Extend the superclass method by calling it from within the subclass method. The subclass method
can perform subclass-specific processing in addition to calling the superclass method.
• In a superclass method, implement a series of steps in a procedure using protected methods. Then
reimplement these steps in a subclass method by redefining the protected methods that are called
from within a public superclass method.
• Redefine the same-named method in the subclass, but use different implementations to perform
the same operation differently on subclass objects.
• Implement abstract superclass methods in the subclass. Abstract superclasses can define methods
with no implementation and rely on subclasses to provide the implementation. For more
information, see “Define an Interface Superclass” on page 12-72.
Subclass methods that override superclass methods must define the same value for the Access
attribute as is defined by the superclass method.
For example, suppose that both superclass and subclass define a method called foo. The subclass
method calls the superclass method and performs other steps in addition to the call to the superclass
method. The subclass method can operate on the specialized parts to the subclass that are not part of
the superclass.
For example, this subclass defines a foo method that calls the superclass foo method
12-13
12 How to Build on Other Classes
% preprocessing steps
...
foo@Super(obj);
% postprocessing steps
...
end
end
end
classdef Super
methods (Sealed)
function foo(obj)
step1(obj) % Call step1
step2(obj) % Call step2
step3(obj) % Call step3
end
end
methods (Access = protected)
function step1(obj)
% Superclass version
end
function step2(obj)
% Superclass version
end
function step3(obj)
% Superclass version
end
end
end
The subclass does not override the foo method. Instead it overrides only the protected methods that
perform the series of steps (step1(obj), step2(obj), step3(obj)). This technique enables the
subclass to specialize the actions taken by each step, but not control the order of the steps in the
process. When you pass a subclass object to the superclass foo method, MATLAB calls the subclass
step methods because of the dispatching rules. For more information on method dispatching, see
“Method Invocation” on page 9-11.
12-14
Modify Inherited Methods
% Subclass version
end
...
end
end
classdef Super
methods
function foo(obj)
% Superclass implementation
end
end
end
classdef Super
methods (Abstract)
foo(obj)
% Abstract method without implementation
end
end
12-15
12 How to Build on Other Classes
See Also
Related Examples
• “Call Superclass Methods on Subclass Objects” on page 5-11
• “Abstract Classes and Class Members” on page 12-68
• “Modify Inherited Properties” on page 12-17
12-16
Modify Inherited Properties
If a superclass defines a property as abstract, the subclass must implement a concrete version of this
property or the subclass is also abstract. Superclasses define abstract properties to create a
consistent interface among subclasses.
If a superclass defines a property with private access, then only the superclass can access this
property. The subclass can implement a different property with the same name.
For example, if a subclass property has the same name as a superclass private property, and a method
of the superclass references the property name, MATLAB accesses the property defined by the
superclass.
classdef Super
properties (Access = private)
Prop = 2
end
methods
function p = superMethod(obj)
p = obj.Prop;
end
end
end
If you create an instance of the Sub class and use it to call the superclass method, MATLAB accesses
the private property of the superclass:
subObj = Sub
12-17
12 How to Build on Other Classes
subObj =
Prop: 1
subObj.superMethod
ans =
See Also
More About
• “Property Attributes” on page 8-8
12-18
Subclassing Multiple Classes
For more information on class syntax, see “Subclass Syntax” on page 12-5.
Here are various situations where you can resolve name and definition conflicts.
Property Conflicts
If two or more superclasses define a property with the same name, then at least one of the following
must be true:
• All, or all but one of the properties must have their SetAccess and GetAccess attributes set to
private
• The properties have the same definition in all superclasses (for example, when all superclasses
inherited the property from a common base class)
Method Conflicts
If two or more superclasses define methods with the same name, then at least one of the following
must be true:
• The method Access attribute is private so only the defining superclass can access the method.
• The method has the same definition in all subclasses. This situation can occur when all
superclasses inherit the method from a common base class and none of the superclasses override
the inherited definition.
• The subclass redefines the method to disambiguate the multiple definitions across all
superclasses. Therefore, the superclass methods must not have their Sealed attribute set to
true.
• Only one superclass defines the method as Sealed, in which case, the subclass adopts the sealed
method definition.
12-19
12 How to Build on Other Classes
• The superclasses define the methods as Abstract and rely on the subclass to define the method.
Event Conflicts
If two or more superclasses define events with the same name, then at least one of the following must
be true:
Multiple Inheritance
Resolving the potential conflicts involved when defining a subclass from multiple classes often
reduces the value of this approach. For example, problems can arise when you enhance superclasses
in future versions and introduce new conflicts.
Reduce potential problems by implementing only one unrestricted superclass. In all other
superclasses, all methods are
• Abstract
• Defined by a subclass
• Inherited from the unrestricted superclass
When using multiple inheritance, ensure that all superclasses remain free of conflicts in definition.
See Also
Related Examples
• “Design Subclass Constructors” on page 12-7
• “Handle Compatible Classes” on page 12-31
12-20
Specify Allowed Subclasses
In this section...
“Why Control Allowed Subclasses” on page 12-21
“Specify Allowed Subclasses” on page 12-21
“Define Sealed Hierarchy of Classes” on page 12-22
The AllowedSubclasses attribute provides a design point between Sealed classes, which do not
allow subclassing, and the default behavior, which does not restrict subclassing.
By controlling the allowed subclasses, you can create a sealed hierarchy of classes. That is, a system
of classes that enables a specific set of classes to derive from specific base classes, but that does not
allow unrestricted subclassing.
See “Define Sealed Hierarchy of Classes” on page 12-22 for more about this technique.
Specify a list of one or more allowed subclasses in the classdef statement by assigning
meta.class objects to the AllowedSubclasses attribute. Create the meta.class object
referencing a specific class using the ? operator and the class name:
Use a cell array of meta.class objects to define more than one allowed subclass:
classdef (AllowedSubclasses = {?ClassName1,?ClassName2,...?ClassNameN}) MySuperClass
...
end
Always use the fully qualified class name when referencing the class name:
classdef (AllowedSubclasses = ?Package.SubPackage.ClassName1) MySuperClass
...
end
Assigning an empty cell array to the AllowedSubclasses attribute is effectively the same as
defining a Sealed class.
12-21
12 How to Build on Other Classes
Note Use only the ? operator and the class name to generate meta.class objects. Values assigned
to the AllowedSubclasses attribute cannot contain any other MATLAB expressions, including
functions that return either meta.class objects or cell arrays of meta.class objects.
Including a class in the list of AllowedSubclasses does not define that class as a subclass or
require you to define the class as a subclass. It just allows the referenced class to be defined as a
subclass. Declaring a class as an allowed subclass also does not affect whether this class can itself be
subclassed.
A class definition can contain assignments to the AllowedSubclasses attribute that reference
classes that are not currently defined or available on the MATLAB path. Any referenced subclass that
MATLAB cannot find when loading the class is effectively removed from the list without causing an
error or warning. MATLAB remembers the referenced class in case it becomes available at a later
point in time.
Note If MATLAB does not find any of the classes in the allowed subclasses list, the class is effectively
Sealed. A sealed class is equivalent to AllowedSubclasses = {}.
Sealed class hierarchies enable you to use the level of abstraction that your design requires while
maintaining a closed system of classes.
See Also
Related Examples
• “Handle Compatible Classes” on page 12-31
12-22
Class Members Access
Basic Knowledge
The material presented in this section builds on an understanding of these concepts:
• Properties — Access, GetAccess, and SetAccess. For a list of all property attributes, see
“Property Attributes” on page 8-8.
• Methods — Access. For a list of all method attributes, see “Method Attributes” on page 9-4.
• Events — ListenAccess and NotifyAccess. For a list of all event attributes, see “Event
Attributes” on page 11-16.
12-23
12 How to Build on Other Classes
This technique provides greater flexibility and control in the design of a system of classes. For
example, use access control lists to define separate classes, but not allow access to class members
from outside the class system.
Specify the classes that are allowed to access a particular class member in the member access
attribute statement. For example:
Use the class meta.class object to refer to classes in the access list. To specify more than one class,
use a cell array of meta.class objects. Use the package name when referring to classes that are in
packages.
Note Specify the meta.class objects explicitly (created with the ? operator), not as values returned
by functions or other MATLAB expressions.
Generate the meta.class objects using only the ? operator and the class name. Values assigned to
the attributes cannot contain any other MATLAB expressions, including functions that return allowed
attribute values:
• meta.class objects
• Cell arrays of meta.class objects
• The values public, protected, or private
Specify these values explicitly, as shown in the example code in this section.
12-24
Class Members Access
classdef GrantAccess
properties (GetAccess = ?NeedAccess)
Prop1 = 7
end
end
The NeedAccess class defines a method that uses the value of the GrantAccess Prop1 value. The
dispObj method is defined as a Static method, however, it could be an ordinary method.
classdef NeedAccess
methods (Static)
function dispObj(GrantAccessObj)
disp(['Prop1 is: ',num2str(GrantAccessObj.Prop1)])
end
end
end
Get access to Prop1 is private so MATLAB returns an error if you attempt to access the property
from outside the class definition. For example, from the command line:
a = GrantAccess;
a.Prop1
NeedAccess.dispObj(a)
Prop1 is: 7
These sample classes show the behavior of methods called from methods of other classes that are in
the access list. The class AcListSuper gives the AcListNonSub class access to its m1 method:
classdef AcListSuper
methods (Access = {?AcListNonSub})
function obj = m1(obj)
disp ('Method m1 called')
end
end
end
12-25
12 How to Build on Other Classes
Because AcListNonSub is in the access list of m1, its methods can call m1 using an instance of
AcListSuper:
classdef AcListNonSub
methods
function obj = nonSub1(obj,AcListSuper_Obj)
% Call m1 on AcListSuper class
AcListSuper_Obj.m1;
end
function obj = m1(obj)
% Define a method named m1
disp(['Method m1 defined by ',class(obj)])
end
end
end
Method m1 called
Including the defining class in the access list for a method grants access to all subclasses derived
from that class. When you derive from a class that has a method with an access list and that list does
not include the defining class:
For example, AcListSub is a subclass of AcListSuper. The AcListSuper class defines an access
list for method m1. However, this list does not include AcListSuper, so subclasses of AcListSuper
do not have access to method m1:
classdef AcListSub < AcListSuper
methods
function obj = sub1(obj,AcListSuper_Obj)
% Access m1 via superclass object (***NOT ALLOWED***)
AcListSuper_Obj.m1;
end
function obj = sub2(obj,AcListNonSub_Obj,AcListSuper_obj)
% Access m1 via object that is in access list (is allowed)
AcListNonSub_Obj.nonSub1(AcListSuper_Obj);
12-26
Class Members Access
end
end
end
Attempting to call the superclass m1 method from the sub1 method results in an error because
subclasses are not in the access list for m1:
a = AcListSuper;
c = AcListSub;
c.sub1(a);
You can call a superclass method from a subclass that does not have access to that method using an
object of a class that is in the superclass method access list.
The AcListSub sub2 method calls a method of a class (AcListNonSub) that is on the access list for
m1. This method, nonSub1, does have access to the superclass m1 method:
a = AcListSuper;
b = AcListNonSub;
c = AcListSub;
c.sub2(b,a);
Method m1 called
When subclasses are not included in the access list for a method, those subclasses cannot define a
method with the same name. This behavior is not the same as cases in which the method Access is
explicitly declared as private.
For example, adding the following method to the AcListSub class definition produces an error when
you attempt to instantiate the class.
methods (Access = {?AcListNonSub})
function obj = m1(obj)
disp('AcListSub m1 method')
end
end
c = AcListSub;
Class 'AcListSub' is not allowed to override the method 'm1' because neither it nor its
superclasses have been granted access to the method by class 'AcListSuper'.
The AcListNonSub class is in the m1 method access list. This class can define a method that calls the
m1 method using an object of the AcListSub class. While AcListSub is not in the access list for
method m1, it is a subclass of AcListSuper.
12-27
12 How to Build on Other Classes
methods
function obj = nonSub2(obj,AcListSub_Obj)
disp('Call m1 via subclass object:')
AcListSub_Obj.m1;
end
end
b = AcListNonSub;
c = AcListSub;
b.nonSub2(c);
This behavior is consistent with the behavior of any subclass object, which can substitute for an
object of its superclass.
When an abstract method has an access list, only the classes in the access list can implement the
method. A subclass that is not in the access list cannot implement the abstract method so that
subclass is itself abstract.
See Also
Related Examples
• “Property Attributes” on page 8-8
• “Method Access List” on page 12-29
• “Event Access List” on page 12-30
12-28
Method Access List
classdef MethodAccess
methods (Access = {?ClassA, ?ClassB, ?MethodAccess})
function listMethod(obj)
...
end
end
end
• Access to listMethod from an instance of MethodAccess by methods of the classes ClassA and
ClassB.
• Access to listMethod from an instance of MethodAccess by methods of subclasses of
MethodAccess, because of the inclusion of MethodAccess in the access list.
• Subclasses of ClassA and ClassB are allowed to define a method named listMethod, and
MethodAccess is allowed to redefine listMethod. However, if MethodAccess was not in the
access list, its subclasses could not redefine listMethod.
See Also
Related Examples
• “Methods with Access Lists” on page 12-25
12-29
12 How to Build on Other Classes
classdef EventAccess
events (NotifyAccess = private, ListenAccess = {?ClassA, ?ClassB})
Event1
Event2
end
end
• Limits notify access for Event1 and Event2 to EventAccess; only methods of the EventAccess
can trigger these events.
• Gives listen access for Event1 and Event2 to methods of ClassA and ClassB. Methods of
EventAccess, ClassA, and ClassB can define listeners for these events. Subclasses of MyClass
cannot define listeners for these events.
See Also
Related Examples
• “Events and Listeners Syntax” on page 11-18
12-30
Handle Compatible Classes
Basic Knowledge
The material presented in this section builds on knowledge of the following information.
Key Concepts
Handle-compatible class — a class that you can include with handle classes in a class hierarchy, even
if the class is not a handle class.
HandleCompatible — the class attribute that defines nonhandle classes as handle compatible.
The solution is to use handle-compatible classes. You can use handle-compatible classes with handle
classes when forming sets of superclasses. Designate a class as handle compatible by using the
HandleCompatible class attribute.
classdef (HandleCompatible) MyClass
...
end
12-31
12 How to Build on Other Classes
• If a class does not explicitly set its HandleCompatible attribute and, if any superclass is a
handle, then all superclasses must be handle compatible.
• The HandleCompatible attribute is not inherited.
A class that does not explicitly set its HandleCompatible attribute to true is:
isa(obj,'handle')
See Also
Related Examples
• “How to Define Handle-Compatible Classes” on page 12-33
12-32
How to Define Handle-Compatible Classes
• It is a handle class
• Its HandleCompatible attribute is set to true
The HandleCompatible class attribute identifies classes that you can combine with handle classes
when specifying a set of superclasses.
Handle compatibility provides greater flexibility when defining abstract superclasses. For example,
when using superclasses that support both handle and value subclasses, handle compatibility
removes the need to define both a handle version and a nonhandle version of a class.
In this example, the Utility class defines a method to reset property values to the default values
defined in the respective class definition. This functionality is useful to both handle and value
subclasses.
classdef (HandleCompatible) Utility
methods
function obj = resetDefaults(obj)
mc = metaclass(obj);
mp = mc.PropertyList;
for k=1:length(mp)
if mp(k).HasDefault && ~strcmp(mp(k).SetAccess,'private')
obj.(mp(k).Name) = mp(k).DefaultValue;
end
end
end
end
end
The Utility class is handle compatible. Therefore, you can use it in the derivation of classes that
are either handle classes or value classes. See “Class Introspection and Metadata” for information on
using meta-data classes.
The resetDefaults method defined by the Utility class returns the object it modifies. When you
call resetDefaults with a value object, the method must return the modified object. It is important
to implement methods that work with both handle and value objects in a handle compatible
superclass. See “Object Modification” on page 5-35 for more information on modifying handle and
value objects.
Consider the behavior of a value class that subclasses the Utility class. The PropertyDefaults
class defines three properties, all of which have default values:
12-33
12 How to Build on Other Classes
pd = PropertyDefaults
pd =
Assign new values that are different from the default values:
pd.p1 = datestr(rem(now,1));
pd.p2 = 'green';
pd.p3 = pi/4;
All pd object property values now contain values that are different from the default values originally
defined by the class:
pd
pd =
Call the resetDefaults method, which is inherited from the Utility class. Because the
PropertyDefaults class is not a handle class, return the modified object.
pd = pd.resetDefaults
pd =
If the PropertyDefaults class was a handle class, then you would not need to save the object
returned by the resetDefaults method. To design a handle compatible class like Utility, ensure
that all methods work with both kinds of classes.
12-34
How to Define Handle-Compatible Classes
However, subclassing a handle-compatible class does not necessarily result in the subclass being
handle compatible. Consider the following two cases, which demonstrate two possible results.
Define a class that subclasses a handle class and the handle-compatible Utility class discussed in
“A Handle Compatible Class” on page 12-33. The HPropertyDefaults class has these
characteristics:
hpd = HPropertyDefaults;
mc = metaclass(hpd);
mc.HandleCompatible
ans =
If you subclass both a value class that is not handle compatible and a handle compatible class, the
subclass is a nonhandle compatible value class. The ValueSub class:
The ValueSub class is a nonhandle-compatible value class because the MException class does not
define the HandleCompatible attribute as true:
12-35
12 How to Build on Other Classes
hv = ValueSub('MATLAB:narginchk:notEnoughInputs',...
'Not enough input arguments.');
mc = metaclass(hv);
mc.HandleCompatible
ans =
See Also
Related Examples
• “Methods for Handle-Compatible Classes” on page 12-37
12-36
Methods for Handle-Compatible Classes
• If an input object is a handle object and the method alters the handle object, these changes are
visible to all workspaces that contain the same handle.
• If an input object is a value object, then changes to the object made inside the method affect only
the value inside the method workspace.
Handle compatible methods generally do not alter input objects because the effects of such changes
are not the same for handle and nonhandle objects.
See “Object Modification” on page 5-35 for information about modifying handle and value objects.
See Also
Related Examples
• “Handle-Compatible Classes and Heterogeneous Arrays” on page 12-38
12-37
12 How to Build on Other Classes
Heterogeneous Arrays
A heterogeneous array contains objects of different classes. Members of a heterogeneous array have
a common superclass, but can belong to different subclasses. See the
matlab.mixin.Heterogeneous class for more information on heterogeneous arrays. The
matlab.mixin.Heterogeneous class is a handle-compatible class.
Subclasses cannot override sealed methods. In situations requiring subclasses to specialize methods
defined by a utility class, you can employ the design pattern referred to as the template method.
Template Technique
Suppose that you implement a handle-compatible class that works with heterogeneous arrays. This
approach enables you to seal public methods, while providing a way for each subclass to specialize
how the method works on each subclass instance. In the handle-compatible class:
Each subclass in the heterogeneous hierarchy implements a concrete version of the abstract method.
The concrete method provides specialized behavior required by the particular subclass.
12-38
Handle-Compatible Classes and Heterogeneous Arrays
printElement(objIn)
end
end
See Also
Related Examples
• “Handle Compatible Classes” on page 12-31
12-39
12 How to Build on Other Classes
In this section...
“MATLAB Built-In Types” on page 12-40
“Built-In Types You Can Subclass” on page 12-40
“Why Subclass Built-In Types” on page 12-40
“Which Functions Work with Subclasses of Built-In Types” on page 12-41
“Behavior of Built-In Functions with Subclass Objects” on page 12-41
“Built-In Subclasses That Define Properties” on page 12-42
Built-in types define methods that perform operations on objects of these classes. For example, you
can perform operations on numeric arrays such as, sorting, arithmetic, and logical operations.
See “Fundamental MATLAB Classes” for more information on MATLAB built-in classes.
Note Defining a class with the same name as a built-in class is not supported.
You cannot subclass any class that has its Sealed attribute set to true. To determine if the class is
Sealed, query the class metadata:
mc = ?ClassName;
mc.Sealed
A value of 0 indicates that the class is not Sealed and can be subclasses.
12-40
Subclasses of MATLAB Built-In Types
To see a list of functions that the subclass has inherited as methods, use the methods function:
methods('SubclassName')
• Inherited methods
• Functions that normally accept input arguments of the same class as the superclass.
When you define a subclass of a built-in class, the subclass inherits all the methods defined by that
built-in class. MATLAB also provides additional methods to subclasses of built-in classes that override
several built-in functions.
Built-in functions and methods that work on built-in classes can behave differently when called with
subclasses of built-in classes. Their behavior depends on which function you are using and whether
your subclass defines properties.
Behavior Categories
When you call an inherited method on a subclass of a built-in class, the result depends on the nature
of the operation performed by the method. The behaviors of these methods fit into several categories.
• Operations on data values return objects of the superclass. For example, if you subclass double
and perform addition on two subclass objects, MATLAB adds the numeric values and returns a
value of class double.
• Operations on the orientation or structure of the data return objects of the subclass. Methods that
perform these kinds of operations include, reshape, permute, transpose, and so on.
• Converting a subclass object to a built-in class returns an object of the specified class. Functions
such as uint32, double, char work with subclass objects the same as they work with built-in
objects.
• Comparing objects or testing for inclusion in a specific set returns logical or built-in objects,
depending on the function. Functions such as isequal, ischar, isobject work with subclass
objects the same as they work with superclass objects.
• Indexing expressions return objects of the subclass. If the subclass defines properties, then
default indexing no longer works. The subclass must define its own indexing methods.
• Concatenation returns an object of the subclass. If the subclass defines properties, then default
concatenation no longer works and the subclass must define its own concatenation methods.
To list the built-in functions that work with a subclass of a built-in class, use the methods function.
12-41
12 How to Build on Other Classes
The subclass must define what indexing and concatenation mean for a class with properties. If your
subclass needs indexing and concatenation functionality, then the subclass must implement the
appropriate methods.
Note Modular indexing mixin classes were introduced in R2021b, but these mixins are not
compatible with subclasses of built-in classes. See “Code Patterns for subsref and subsasgn Methods”
on page 17-9 for more information on how to implement subsref, subsasgn, and subsindex.
See Also
Related Examples
• “Subclasses of Built-In Types with Properties” on page 12-53
• “Subclasses of Built-In Types Without Properties” on page 12-47
12-42
Behavior of Inherited Built-In Methods
Subclass double
Most built-in functions used with built-in classes are actually methods of the built-in class. For
example, the double and single classes define several methods to perform arithmetic operations,
indexing, matrix operation, and so on. All these built-in class methods work with subclasses of the
built-in class.
Subclassing double enables your class to use features without implementing the methods that a
MATLAB built-in class defines.
sc =
1x10 DocSimpleDouble:
double data:
1 2 3 4 5 6 7 8 9 10
Call a method inherited from class double that operates on the data, such as sum. sum returns a
double and, therefore, uses the display method of class double:
sum(sc)
ans =
55
Index sc like an array of doubles. The returned value is the class of the subclass:
a = sc(2:4)
a =
1x3 DocSimpleDouble:
12-43
12 How to Build on Other Classes
double data:
2 3 4
Indexed assignment works the same as the built-in class. The returned value is the class of the
subclass:
sc(1:5) = 5:-1:1
sc =
1x10 DocSimpleDouble:
double data:
5 4 3 2 1 6 7 8 9 10
Calling a method that modifies the order of the data elements operates on the data, but returns an
object of the subclass:
sc = DocSimpleDouble(1:10);
sc(1:5) = 5:-1:1;
a = sort(sc)
a =
1x10 DocSimpleDouble:
double data:
1 2 3 4 5 6 7 8 9 10
sc = DocSimpleDouble(1:10);
a = sin(sc);
class(a)
ans =
double
sc = DocSimpleDouble(randi(9,1,10))
sc = DocSimpleDouble(randi(9,1,10))
sc =
1x10 DocSimpleDouble:
double data:
6 1 8 9 7 7 7 4 6 2
b = sort(sc)
12-44
Behavior of Inherited Built-In Methods
b =
1x10 DocSimpleDouble:
double data:
1 2 4 6 6 7 7 7 8 9
• reshape
• permute
• sort
• transpose
• ctranspose
For example, indexing element 2 in the DocSimpleDouble subclass object returns the second
element in the vector.
sc = DocSimpleDouble(1:10);
a = sc(2)
a =
DocSimpleDouble
double data:
2
Assigning a new value to the second element in the DocSimpleDouble object operates only on the
superclass data.
sc(2) = 12
sc =
1x10 DocSimpleDouble:
double data:
1 12 3 4 5 6 7 8 9 10
12-45
12 How to Build on Other Classes
ans =
1x20 DocSimpleDouble:
double data:
Columns 1 through 13
1 2 3 4 5 6 7 8 9 10 11 12 13
Columns 14 through 20
14 15 16 17 18 19 20
[sc1;sc2]
ans =
2x10 DocSimpleDouble:
double data:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
c = cat(3,sc1,sc2)
c =
1x10x2 DocSimpleDouble:
double data:
(:,:,1) =
1 2 3 4 5 6 7 8 9 10
(:,:,2) =
11 12 13 14 15 16 17 18 19 20
If the subclass of a built-in class defines properties, you cannot concatenate objects of the subclass.
There is no way to determine how to combine properties of different objects. However, your subclass
can define custom horzcat and vertcat methods to support concatenation in whatever way makes
sense for your subclass.
See Also
Related Examples
• “Subclasses of Built-In Types Without Properties” on page 12-47
• “Subclasses of Built-In Types with Properties” on page 12-53
12-46
Subclasses of Built-In Types Without Properties
In this section...
“Specialized Numeric Types” on page 12-47
“A Class to Manage uint8 Data” on page 12-47
“Using the DocUint8 Class” on page 12-48
For more information, see “Subclasses of MATLAB Built-In Types” on page 12-40.
• Capability to convert various classes of image data to uint8 to reduce object data storage.
• A method to display the intensity images contained in the subclass objects.
• Ability to use all the methods supported by uint8 data (for example, size, indexing, reshape,
bitshift, cat, fft, arithmetic operators, and so on).
The class data are matrices of intensity image data stored in the superclass part of the subclass
object. This approach requires no properties.
The DocUint8 class stores the image data, which converts the data, if necessary:
12-47
12 How to Build on Other Classes
The DocUint8 class provides a method to display all images stored as DocUint8 objects in a
consistent way. For example:
cir = imread('circuit.tif');
img1 = DocUint8(cir);
img1.showImage;
Because DocUint8 subclasses uint8, you can use any uint8 methods. For example,
size(img1)
ans =
280 272
Indexing Operations
Inherited methods perform indexing operations, but return objects of the same class as the subclass.
Therefore, you can index into the image data and call a subclass method:
showImage(img1(100:200,1:160));
12-48
Subclasses of Built-In Types Without Properties
Subscripted reference operations (controlled by the inherited subsref method) return a DocUint8
object.
img1(100:120,140:160) = 255;
img1.showImage;
12-49
12 How to Build on Other Classes
Concatenation Operations
Concatenation operations work on DocUint8 objects because this class inherits the uint8 horzcat
and vertcat methods, which return a DocUint8 object:
showImage([img1 img1]);
12-50
Subclasses of Built-In Types Without Properties
Data Operations
Methods that operate on data values, such as arithmetic operators, always return an object of the
built-in type (not of the subclass type). For example, multiplying DocUint8 objects returns a uint8
object, so calling showImage throws an error:
a = img1.*1.8;
showImage(a);
Check for missing argument or incorrect argument data type in call to function 'showImage'.
To perform operations of this type, implement a subclass method to override the inherited method.
The times method implements array (element-by-element) multiplication.
function o = times(obj,val)
u8 = uint8(obj).*val;
o = DocUint8(u8);
end
When you override a uint8 method, MATLAB calls the subclass method, not the base class method.
The subclass method must:
12-51
12 How to Build on Other Classes
After adding the times method to the DocUint8 class, the output of multiplication expressions is an
object of the DocUint8 class:
showImage(img1.*1.8);
See Also
Related Examples
• “Operator Overloading” on page 17-19
• “Subclasses of Built-In Types with Properties” on page 12-53
12-52
Subclasses of Built-In Types with Properties
Providing additional data storage in the subclass by defining properties can be a useful extension to
the built-in data class. However, the addition of properties to the subclass requires the subclass to
define methods to implement standard array behaviors.
For more information, see “Subclasses of MATLAB Built-In Types” on page 12-40.
This sample implementation of the ExtendDouble class derives from the double class and defines a
single property. The ExtendDouble class definition demonstrates how to implement indexing and
concatenation for subclasses of built-in classes
Property Added
The ExtendDouble class defines the DataString property to contain text that describes the data.
The superclass part of the class contains the numeric data.
Methods Implemented
The following methods modify the behavior of the ExtendDouble class:
12-53
12 How to Build on Other Classes
• subsasgn — Enables subscripted assignment to the superclass part of the subclass, dot notation
reference to the DataString property, and dot notation reference the built-in data via the name
Data.
• horzcat — Defines horizontal concatenation of ExtendDouble objects. concatenates the
superclass part using the double class horzcat method and forms a cell array of the
DataString properties.
• vertcat — The vertical concatenation equivalent of horzcat (both are required).
• char — A ExtendDouble to char converter used by horzcat and vertcat.
• disp — ExtendDouble implements a disp method to provide a custom display for the object.
properties
DataString
end
methods
function obj = ExtendDouble(data,str)
if nargin == 0
data = 0;
str = '';
elseif nargin == 1
str = '';
end
obj = obj@double(data);
obj.DataString = str;
end
12-54
Subclasses of Built-In Types with Properties
end
end
function disp(obj)
disp(obj.DataString)
disp(double(obj))
end
end
end
Using ExtendDouble
Create an instance of ExtendDouble and notice that the display is different from the default:
12-55
12 How to Build on Other Classes
ed = ExtendDouble(1:10,'One to ten')
ed =
One to ten
1 2 3 4 5 6 7 8 9 10
Inherited Methods
The ExtendDouble class inherits methods from the class double. To see a list of all public methods
defined by the double class, use the methods function:
methods(double.empty)
The sum function continues to operate on the superclass part of the object:
sum(ed)
ans =
55
sort(ed(10:-1:1))
ans =
1 2 3 4 5 6 7 8 9 10
ed.^2
ans =
1 4 9 16 25 36 49 64 81 100
Subscripted Indexing
Because the ExtendDouble class defines a property, the class must implement its own subsref and
subsasgn methods.
This class implements the following subscripted indexing expressions for reference and assignment.
The class subsref method enables you to use ExtendDouble objects like numeric arrays to
reference the numeric data:
ed = ExtendDouble(1:10,'One to ten');
ed(10:-1:1)
ans =
12-56
Subclasses of Built-In Types with Properties
One to ten
10 9 8 7 6 5 4 3 2 1
Access the numeric data of the ExtendDouble using property-style indexing with the arbitrarily
chosen name Data:
ed.Data(10:-1:1)
ans =
One to ten
10 9 8 7 6 5 4 3 2 1
ans =
One to ten
One to thirteen'
1 2 3 4 5 6 7 8 9 10 11 12 13
The ExtendDouble inherits converter methods from the double class. For example, MATLAB calls
the char method to perform this assignment statement.
ed(11:13) = ['a','b','c']
ed =
one to thirteen
1 2 3 4 5 6 7 8 9 10 97 98 99
The ExtendDouble implements two forms of indexed reference in the subsref method:
a =
One to ten
1
b = ed.Data(1)
12-57
12 How to Build on Other Classes
b =
whos
The increased flexibility of the implementation of indexed reference in the ExtendDouble class.
hcat = [ed1,ed2]
hcat =
Columns 1 through 13
1 2 3 4 5 6 7 8 9 10 10 9 8
Columns 14 through 20
7 6 5 4 3 2 1
whos
vcat = [ed1;ed2]
vcat =
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
Both horzcat and vertcat return a new object of the same class as the subclass.
12-58
Subclasses of Built-In Types with Properties
See Also
Related Examples
• “Subclasses of Built-In Types Without Properties” on page 12-47
12-59
12 How to Build on Other Classes
The size and numel functions work consistently with arrays of user-defined objects. There is
generally no need to overload size or numel in user-defined classes.
Several MATLAB functions use size and numel to perform their operations. Therefore, if you do
overload either of these functions in your class, be sure that objects of your class work as designed
with other MATLAB functions.
If your class modifies array indexing, see “Overload numArgumentsFromSubscript Instead of numel”
on page 12-64
d = 1:10;
size(d)
ans =
1 10
numel(d)
ans =
10
dsub = d(7:end);
size(dsub)
ans =
1 4
12-60
Use of size and numel with Classes
sd = SimpleDouble(1:10);
size(sd)
ans =
1 10
The numel function returns the number of elements in the superclass part:
numel(sd)
ans =
10
size([sd;sd])
ans =
2 10
numel([sd;sd])
ans =
20
The SimpleDouble class inherits the indexing behavior of the double class:
sdsub = sd(7:end);
size(sdsub)
12-61
12 How to Build on Other Classes
ans =
1 4
classdef VerySimpleClass
properties
Value
end
end
vs = VerySimpleClass;
vs.Value = 1:10;
size(vs)
ans =
1 1
numel(vs)
ans =
size([vs;vs])
ans =
2 1
numel([vs;vs])
ans =
size(vs.Value)
ans =
1 10
vssub = vs.Value(7:end);
size(vssub)
ans =
1 4
12-62
Use of size and numel with Classes
class(vs.Value)
ans =
double
vsArray(1:10) = VerySimpleClass;
isempty([vsArray(2:10).Value])
ans =
MATLAB does not apply scalar expansion to object array property value assignment. Use the deal
function for this purpose:
[vsArray.Value] = deal(1:10);
isempty([vsArray.Value])
ans =
The deal function assigns values to each Value property in the vsArray object array.
Indexing rules for object arrays are equivalent to the rules for arrays of struct:
vsArray(1).Value
ans =
1 2 3 4 5 6 7 8 9 10
vsArray(1).Value(6)
ans =
Other MATLAB functions use the values returned by these functions. If you change the way that size
and numel behave, ensure that the values returned make sense for the intended use of your class.
12-63
12 How to Build on Other Classes
A(1:2).Prop
Subclasses of built-in classes always return scalar objects as a result of subscripted reference and
always use scalar objects for subscripted assignment.
If you define a class in which nargout for subsref or nargin for subsasgn must be a specific
value, then overload numArgumentsFromSubscript to return that value.
See Also
numArgumentsFromSubscript
12-64
Determine Array Class
a = [2,5,7,11];
class(a)
ans =
double
ans =
char
a = [2,5,7,11];
isa(a,'double')
ans =
1
isa(a,'float')
ans =
1
isa(a,'numeric')
ans =
1
isa returns true for classes derived from the specified class. For example, the SubInt class derives
from the built-in type int16:
12-65
12 How to Build on Other Classes
By definition, an instance of the SubInt class is also an instance of the int16 class:
aInt = SubInt;
isa(aInt,'int16')
ans =
1
isa(aInt,'integer')
ans =
1
class(aInt)
ans =
SubInt
Use the strcmp function with the class function to check for a specific class of an object:
a = int16(7);
strcmp(class(a),'int16')
ans =
1
Because the class function returns the class name as a char vector, the inheritance does not affect
the result of the comparison performed by strcmp:
aInt = SubInt;
strcmp(class(aInt),'int16')
ans =
0
12-66
Determine Array Class
Use the following techniques to exclude subclasses of built-in types from the input arguments.
• Define a cell array that contains the names of built-in types accepted by your function.
• Call class and strcmp to test for specific types in a MATLAB control statement.
if strcmp(class(inputArg),'single')
% Call function
else
inputArg = single(inputArg);
end
Suppose that you create a MEX-function, myMexFcn, that requires two numeric inputs that must be of
type double or single:
outArray = myMexFcn(a,b)
Define a cell array that contains the character arrays double and single:
floatTypes = {'double','single'};
Use isobject to separate built-in types from subclasses of built-in types. The isobject function
returns false for instances of built-in types:
ans =
0
12-67
12 How to Build on Other Classes
Abstract Classes
Abstract classes are useful for describing functionality that is common to a group of classes, but
requires unique implementations within each class.
abstract class — A class that cannot be instantiated, but that defines class components used by
subclasses.
concrete class — A class that can be instantiated. Concrete classes contain no abstract members.
interface — An abstract class describing functionality that is common to a group of classes, but that
requires unique implementations within each class. The abstract class defines the interface of each
subclass without specifying the actual implementation.
An abstract class serves as a basis (that is, a superclass) for a group of related subclasses. An
abstract class can define abstract properties and methods that subclasses implement. Each subclass
can implement the concrete properties and methods in a way that supports their specific
requirements.
A subclass must implement all inherited abstract properties and methods to become a concrete class.
Otherwise, the subclass is itself an abstract class.
MATLAB does not force subclasses to implement concrete methods with the same signature or
attributes.
Abstract classes:
12-68
Abstract Classes and Class Members
If a subclass of an abstract class does not define concrete implementations for all inherited abstract
methods or properties, it is also abstract.
Abstract Class
• Concrete subclasses must redefine any properties or methods that are declared as abstract.
• The abstract class does not need to define any abstract methods or properties.
When you define any abstract methods or properties, MATLAB automatically sets the class Abstract
attribute to true.
Abstract Methods
methods (Abstract)
abstMethod(obj)
end
• Do not use a function...end block to define an abstract method, use only the method
signature.
• Abstract methods have no implementation in the abstract class.
• Concrete subclasses are not required to support the same number of input and output arguments
and do not need to use the same argument names. However, subclasses generally use the same
signature when implementing their version of the method.
• Abstract methods cannot define arguments blocks.
Abstract Properties
properties (Abstract)
AbsProp
end
• Concrete subclasses must redefine abstract properties without the Abstract attribute.
• Concrete subclasses must use the same values for the SetAccess and GetAccess attributes as
those attributes used in the abstract superclass.
12-69
12 How to Build on Other Classes
• Abstract properties cannot define access methods and cannot specify initial values. The subclass
that defines the concrete property can create access methods and specify initial values.
For more information on access methods, see “Property Get and Set Methods” on page 8-38.
classdef AbsClass
methods(Abstract)
result = absMethodOne(obj)
output = absMethodTwo(obj)
end
end
Use the logical value of the meta.class Abstract property to determine if the class is abstract:
mc = ?AbsClass;
if ~mc.Abstract
% not an abstract class
end
Use the meta.abstractDetails function to display the names of abstract properties or methods
and the names of the defining classes:
meta.abstractDetails('AbsClass');
For example, suppose that you create a subclass of the AbsClass class that is defined in the previous
section. In this case, the subclass implements only one of the abstract methods defined by AbsClass.
12-70
Abstract Classes and Class Members
meta.abstractDetails(?SubAbsClass)
The SubAbsClass class is abstract because it has not implemented the absMethodOne method
defined in AbsClass.
msub = ?SubAbsClass;
msub.Abstract
ans =
If you implement both methods defined in AbsClass, the subclass becomes concrete.
See Also
Related Examples
• “Define an Interface Superclass” on page 12-72
12-71
12 How to Build on Other Classes
Interfaces
The properties and methods defined by a class form the interface that determines how class users
interact with objects of the class. When creating a group of related classes, interfaces define a
common interface to all these classes. The actual implementations of the interface can differ from one
class to another.
Consider a set of classes designed to represent various types of graphs. All classes must implement a
Data property to contain the data used to generate the graph. However, the form of the data can
differ considerably from one type of graph to another. Each class can implement the Data property
differently.
The same differences apply to methods. All classes can have a draw method that creates the graph,
but the implementation of this method changes with the type of graph.
The basic idea of an interface class is to specify the properties and methods that each subclass must
implement without defining the actual implementation. This approach enables you to enforce a
consistent interface to a group of related objects. As you add more classes in the future, the interface
remains the same.
This approach enforces the use of a consistent interface while providing the necessary flexibility to
implement the internal workings of each specialized subclass differently.
In this example, a package folder contains the interface, derived subclasses, and a utility function:
+graphics/GraphInterface.m % abstract interface class
+graphics/LineGraph.m % concrete subclass
The GraphInterface class specifies the following properties, which the subclasses must define:
• Primitive — Handle of the graphics object used to implement the specialized graph. The class
user has no need to access these objects directly so this property has protected SetAccess and
GetAccess.
• AxesHandle — Handle of the axes used for the graph. The specialized graph objects can set axes
object properties. This property has protected SetAccess and GetAccess.
• Data — All subclasses of the GraphInterface class must store data. The type of data varies and
each subclass defines the storage mechanism. Subclass users can change the data values so this
property has public access rights.
12-72
Define an Interface Superclass
The GraphInterface class names three abstract methods that subclasses must implement. The
GraphInterface class also suggests in comments that each subclass constructor must accept the
plot data and property name/property value pairs for all class properties.
• Subclass constructor — Accept data and P/V pairs and return an object.
• draw — Used to create a drawing primitive and render a graph of the data according to the type
of graph implemented by the subclass.
• zoom — Implementation of a zoom method by changing the axes CameraViewAngle property. The
interface suggests the use of the camzoom function for consistency among subclasses. The zoom
buttons created by the addButtons static method use this method as a callback.
• updateGraph — Method called by the set.Data method to update the plotted data whenever the
Data property changes.
The package of classes that derive from the GraphInterface abstract class implement the following
behaviors:
The GraphInterface class is an abstract class that defines the methods and properties used by the
subclasses. Comments in the abstract class describe the intended implementation:
12-73
12 How to Build on Other Classes
end
methods
function set.Data(obj,newdata)
obj.Data = newdata;
updateGraph(obj)
end
function addButtons(gobj)
hfig = get(gobj.AxesHandle,'Parent');
uicontrol(hfig,'Style','pushbutton','String','Zoom Out',...
'Callback',@(src,evnt)zoom(gobj,.5));
uicontrol(hfig,'Style','pushbutton','String','Zoom In',...
'Callback',@(src,evnt)zoom(gobj,2),...
'Position',[100 20 60 20]);
end
end
end
The GraphInterface class implements the property set method (set.Data) to monitor changes to
the Data property. An alternative is to define the Data property as Abstract and enable the
subclasses to determine whether to implement a set access method for this property. The
GraphInterface class defines a set access method that calls an abstract method (updateGraph,
which each subclass must implement). The GraphInterface interface imposes a specific design on
the whole package of classes, without limiting flexibility.
The addButtons method adds push buttons for the zoom methods, which each subclass must
implement. Using a method instead of an ordinary function enables addButtons to access the
protected class data (the axes handle). Use the object zoom method as the push-button callback.
function addButtons(gobj)
hfig = get(gobj.AxesHandle,'Parent');
uicontrol(hfig,'Style','pushbutton',...
'String','Zoom Out',...
'Callback',@(src,evnt)zoom(gobj,.5));
uicontrol(hfig,'Style','pushbutton',...
'String','Zoom In',...
'Callback',@(src,evnt)zoom(gobj,2),...
'Position',[100 20 60 20]);
end
This example defines only a single subclass used to represent a simple line graph. It derives from
GraphInterface, but provides implementations for the abstract methods draw, zoom,
updateGraph, and its own constructor. The base class GraphInterface and subclass are all
contained in a package (graphics), which you must use to reference the class name:
classdef LineGraph < graphics.GraphInterface
Add Properties
The LineGraph class implements the interface defined in the GraphInterface class and adds two
additional properties—LineColor and LineType. This class defines initial values for each property,
so specifying property values in the constructor is optional. You can create a LineGraph object with
no data, but you cannot produce a graph from that object.
12-74
Define an Interface Superclass
properties
LineColor = [0 0 0];
LineType = '-';
end
The constructor accepts a struct with x and y coordinate data, and property name/property value
pairs:
function gobj = LineGraph(data,varargin)
if nargin > 0
gobj.Data = data;
if nargin > 2
for k=1:2:length(varargin)
gobj.(varargin{k}) = varargin{k+1};
end
end
end
end
The LineGraph draw method uses property values to create a line object. The LineGraph class
stores the line handle as protected class data. To support the use of no input arguments for the
class constructor, draw checks the Data property to determine if it is empty before proceeding:
function gobj = draw(gobj)
if isempty(gobj.Data)
error('The LineGraph object contains no data')
end
h = line(gobj.Data.x,gobj.Data.y,...
'Color',gobj.LineColor,...
'LineStyle',gobj.LineType);
gobj.Primitive = h;
gobj.AxesHandle = get(h,'Parent');
end
The LineGraph zoom method follows the comments in the GraphInterface class which suggest
using the camzoom function. camzoom provides a convenient interface to zooming and operates
correctly with the push buttons created by the addButtons method.
Property set methods provide a convenient way to execute code automatically when the value of a
property changes for the first time in a constructor. (See “Property Get and Set Methods” on page 8-
38.) The linegraph class uses set methods to update the line primitive data (which causes a
redraw of the plot) whenever a property value changes. The use of property set methods provides a
way to update the data plot quickly without requiring a call to the draw method. The draw method
updates the plot by resetting all values to match the current property values.
Three properties use set methods: LineColor, LineType, and Data. LineColor and LineType are
properties added by the LineGraph class and are specific to the line primitive used by this class.
Other subclasses can define different properties unique to their specialization (for example,
FaceColor).
12-75
12 How to Build on Other Classes
The GraphInterface class implements the Data property set method. However, the
GraphInterface class requires each subclass to define a method called updateGraph, which
handles the update of plot data for the specific drawing primitive used.
methods
function gobj = LineGraph(data,varargin)
if nargin > 0
gobj.Data = data;
if nargin > 1
for k=1:2:length(varargin)
gobj.(varargin{k}) = varargin{k+1};
end
end
end
end
function zoom(gobj,factor)
camzoom(gobj.AxesHandle,factor)
end
function updateGraph(gobj)
set(gobj.Primitive,...
'XData',gobj.Data.x,...
'YData',gobj.Data.y)
end
function set.LineColor(gobj,color)
gobj.LineColor = color;
set(gobj.Primitive,'Color',color)
end
function set.LineType(gobj,ls)
gobj.LineType = ls;
set(gobj.Primitive,'LineStyle',ls)
end
end
end
12-76
Define an Interface Superclass
The LineGraph class defines the simple API specified by the graph base class and implements its
specialized type of graph:
d.x = 1:10;
d.y = rand(10,1);
lg = graphics.LineGraph(d,'LineColor','b','LineType',':');
lg.draw;
lg.addButtons;
Clicking the Zoom In button shows the zoom method providing the callback for the button.
d.y = rand(10,1);
lg.Data = d;
lg.LineColor = [0.9,0.1,0.6];
12-77
12 How to Build on Other Classes
See Also
Related Examples
• “Abstract Classes and Class Members” on page 12-68
12-78
13
• The full name of the object class, including any package qualifiers
• Values of dynamic properties
• All property default values defined by the class at the time the first object of the class is saved to
the MAT-file.
• The names and values of all properties, with the following exceptions:
• Properties are not saved if their current values are the same as the default values specified in
the class definition.
• Properties are not saved if their Transient, Constant, or Dependent attributes set to true.
13-2
Save and Load Process for Objects
• If a property of an object being loaded contains an object, then load creates a new object of the
same class and assigns it to the property. If the object contained in the property is a handle object,
then the property contains a new handle object of the same class.
MATLAB calls property set methods to ensure that property values are still valid in cases where the
class definition has changed.
For information, see “Property Get and Set Methods” on page 8-38 and “Validate Property Values” on
page 8-18.
When an error occurs while an object is being loaded from a file, MATLAB does one of the following:
• If the class defines a loadobj method, MATLAB returns the saved values to the loadobj method
in a struct.
• If the class does not define a loadobj method, MATLAB silently ignores the errors. The load
function reconstitutes the object with property values that do not produce an error.
In the struct passed to the loadobj method, the field names correspond to the property names.
The field values are the saved values for the corresponding properties.
If the saved object derives from multiple superclasses that have private properties with same name,
the struct contains only the property value of the most direct superclass.
For information on how to implement saveobj and loadobj methods, see “Modify the Save and
Load Process” on page 13-12.
If a class definition changes property validation such that loaded property values are no longer valid,
MATLAB substitutes the currently defined default value for that property. The class can define a
loadobj method or converter methods to provide compatibility among class versions.
For information on property validation, see “Validate Property Values” on page 8-18
See Also
isequal
Related Examples
• “Object Save and Load”
13-3
13 Saving and Loading Objects
In this section...
“Default Values” on page 13-4
“Dependent Properties” on page 13-4
“Transient Properties” on page 13-4
“Avoid Saving Unwanted Variables” on page 13-4
Default Values
If a property often has the same value, define a default value for that property. When the user saves
the object to a MAT-file, MATLAB does not save the value of a property if the current value equals the
default value. MATLAB saves the default value on a per class basis to avoid saving the value for every
object.
For more information on how MATLAB evaluates default value expressions, see “Define Properties
with Default Values” on page 8-13.
Dependent Properties
Use a dependent property when the property value must be calculated at run time. A dependent
property is not saved in the MAT-file when you save an object. Instances of the class do not allocate
memory to hold a value for a dependent property.
Dependent is a property attribute (see “Property Attributes” on page 8-8 for a complete list.)
Transient Properties
MATLAB does not store the values of transient properties. Transient properties can store data in the
object temporarily as an intermediate computation step or for faster retrieval. Use transient
properties when you easily can reproduce the data at run time or when the data represents
intermediate state that can be discarded.
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-4
Save Object Data to Recreate Graphics Objects
What to Save
Use transient properties to avoid saving what you can recreate when loading the object. For example,
an object can contain component parts that you can regenerate from data that is saved. Regenerating
these components also enables newer versions of the class to create the components in a different
way.
The Chart property contains the handle to the bar chart. When you save a bar chart, MATLAB also
saves the figure, axes, and Bar object and the data required to create these graphics objects. The
YearlyRainfall class design eliminates the need to save objects that it can regenerate:
• The Chart property is Transient so the graphics objects are not saved.
• ChartData is a private property that provides storage for the Bar object data (YData).
• The load function calls the set.ChartData method, passing it the saved bar chart data.
• The setup method regenerates the bar chart and assigns the handle to the Chart property. Both
the class constructor and the set.ChartData method call setup.
13-5
13 Saving and Loading Objects
end
end
methods(Access = private)
function setup(rf,data)
rf.Chart = bar(data);
end
end
end
For example, change the type of graph from a bar chart to a stair-step graph by modifying the setup
method:
methods(Access = private)
function setup(rf,data)
rf.Chart = stairs(data);
end
end
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-6
Improve Version Compatibility with Default Values
Version Compatibility
Default property values can help you implement version compatibility for saved objects. For example,
suppose that you add a property to version 2 of your class. Having a default value enables MATLAB to
assign a value to the new property when loading a version 1 object.
Similarly, suppose version 2 of your class removes a property. If a version 2 object is saved and loaded
into version 1, your loadobj method can use the default value from version 1.
classdef EmployeeInfo
properties
Name
JobTitle
Department
end
end
Version 2 of the EmployeeInfo class adds a property, Country, for the country name of the
employee location. The Country property has a default value of 'USA'.
classdef EmployeeInfo
properties
Name
JobTitle
Department
Country = 'USA'
end
end
• MATLAB assigns an empty double [] to properties that do not have default values defined by the
class. Empty double is not a valid value for the Country property.
• In version 1, all employees were in the USA. Therefore, any version 1 object loaded into version 2
receives a valid value for the Country property.
13-7
13 Saving and Loading Objects
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-8
Avoid Property Initialization Order Dependency
Suppose that your class design is such that both of the following are true:
Then the final state of an object after changing a series of property values can depend on the order in
which you set the properties. This order dependency can affect the result of loading an object.
The load function sets property values in a particular order. This order can be different from the
order in which you set the properties in the saved object. As a result, the loaded object can have
different property values than the object had when it was saved.
If a property set function changes the values of other properties, then define the Dependent
attribute of that property as true. MATLAB does not save or restore dependent property values.
Use nondependent properties for storing the values set by the dependent property. Then the load
function restores the nondependent properties with the same values that were saved. The load
function does not call the dependent property set method because there is no value in the saved file
for that property.
• The Units property is dependent. Its property set method sets the TotalDistance property.
Therefore load does not call the Units property set method.
• The load function restores TotalDistance to whatever value it had when you saved the object.
classdef Odometer
properties(Constant)
ConversionFactor = 1.6
end
properties
TotalDistance = 0
end
13-9
13 Saving and Loading Objects
properties(Dependent)
Units
end
properties(Access=private)
PrivateUnits = 'mi'
end
methods
function unit = get.Units(obj)
unit = obj.PrivateUnits;
end
function obj = set.Units(obj,newUnits)
% validate newUnits to be a char vector
switch(newUnits)
case 'mi'
if strcmp(obj.PrivateUnits,'km')
obj.TotalDistance = obj.TotalDistance / ...
obj.ConversionFactor;
obj.PrivateUnits = newUnits;
end
case 'km'
if strcmp(obj.PrivateUnits,'mi')
obj.TotalDistance = obj.TotalDistance * ...
obj.ConversionFactor;
obj.PrivateUnits = newUnits;
end
otherwise
error('Odometer:InvalidUnits', ...
'Units ''%s'' is not supported.', newUnits);
end
end
end
end
Suppose that you create an instance of Odometer and set the following property values:
odObj = Odometer;
odObj.Units = 'km';
odObj.TotalDistance = 16;
If the Units property was not Dependent, loading it calls its set method and causes the
TotalDistance property to be set again.
13-10
Avoid Property Initialization Order Dependency
The class avoids order dependence when initializing property values during the load process by
making the TripDistance property dependent. MATLAB does not save or load a value for the
TripDistance property, but does save and load values for the two properties used to calculate
TripDistance in its property get method.
classdef Odometer2
properties
TotalDistance = 0
TripMarker = 0
end
properties(Dependent)
TripDistance
end
methods
function distance = get.TripDistance(obj)
distance = obj.TotalDistance - obj.TripMarker;
end
end
end
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-11
13 Saving and Loading Objects
• Rename a class
• Remove properties
• Define a circular reference of handle objects where initialization order is important
• Must call the constructor with arguments and, therefore, cannot use ConstructOnLoad
The save function calls your class saveobj method before performing the save operation. The save
function then saves the value returned by the saveobj method. You can use saveobj to return a
modified object or a struct that contains property values.
load calls your class loadobj method after loading the object. The load function loads the value
returned by the loadobj method into the workspace. A loadobj method can modify the object being
loaded or can reconstruct an object from the data saved by the class saveobj method.
Implement the loadobj method as a Static method because MATLAB can call the loadobj method
with a struct instead of an object of the class.
Implement the saveobj method as an ordinary method (that is, calling it requires an instance of the
class).
MATLAB saves the object class name so that load can determine which loadobj method to call in
cases where your saveobj method saves only the object data in a structure. Therefore, the class
must be accessible to MATLAB when you load the object.
13-12
Modify the Save and Load Process
• The class definition has changed since the object was saved, requiring you to modify the object
before loading.
• A saveobj method modified the object during the save operation, possibly saving data in a
struct. Implement the loadobj method to reconstruct the object from the output of saveobj.
Additional Considerations
When you decide to modify the default save and load process, keep the following points in mind:
• If loading any property value from the MAT-file produces an error, load passes a struct to
loadobj. The struct field names correspond to the property names extracted from the file.
• loadobj must always be able to accept a struct as input and return an object, even if there is
no saveobj or saveobj does not return a struct.
• If saveobj returns a struct, then load always passes that struct to loadobj.
• Subclass objects inherit superclass loadobj and saveobj methods. Therefore, if you do not
implement a loadobj or saveobj method in the subclass, MATLAB calls only the inherited
methods.
If a superclass implements a loadobj or saveobj method, then a subclass can also implement a
loadobj or saveobj method that calls the superclass methods. For more information, see “Save
and Load Objects from Class Hierarchies” on page 13-24.
• The load function does not call the constructor by default. For more information, see “Initialize
Objects When Loading” on page 13-22.
• Customizing loadobj and saveobj is not supported for enum classes.
See Also
Related Examples
• “Basic saveobj and loadobj Pattern” on page 13-14
• “Object Save and Load”
13-13
13 Saving and Loading Objects
• Use saveobj to save all essential data in a struct and do not save the entire object.
• Use loadobj to reconstruct the object from the saved data.
This approach is not useful in cases where you cannot save property values in a struct field. Data
that you cannot save, such as a file identifier, you can possibly regenerate in the loadobj method.
If you implement a saveobj method without implementing a loadobj method, MATLAB loads a
default object of the class using the current class definition. Add a loadobj method to the class to
create an object using the data saved with the saveobj method.
saveobj
For this pattern, define saveobj as an ordinary method that accepts an object of the class and
returns a struct.
methods
function s = saveobj(obj)
s.Prop1 = obj.Prop1;
s.Prop2 = obj.Prop2
s.Data = obj.GraphHandle.YData;
end
end
loadobj
Define loadobj as a static method. Create an object by calling the class constructor. Then assign
values to properties from the struct passed to loadobj. Use the data to regenerate properties that
were not saved.
methods(Static)
function obj = loadobj(s)
if isstruct(s)
newObj = ClassConstructor;
newObj.Prop1 = s.Prop1;
newObj.Prop2 = s.Prop2
13-14
Basic saveobj and loadobj Pattern
newObj.GraphHandle = plot(s.Data);
obj = newObj;
else
obj = s;
end
end
end
If the load function encounters an error, load passes loadobj a struct instead of an object. Your
loadobj method must always be able to handle a struct as the input argument. The input to
loadobj is always a scalar.
The GraphExpression class creates a graph of a MATLAB expression over a specified range of data.
GraphExpression uses its loadobj method to regenerate the graph, which is not saved with the
object.
classdef GraphExpression
properties
FuncHandle
Range
end
methods
function obj = GraphExpression(fh,rg)
obj.FuncHandle = fh;
obj.Range = rg;
makeGraph(obj)
end
function makeGraph(obj)
rg = obj.Range;
x = min(rg):max(rg);
data = obj.FuncHandle(x);
plot(data)
end
end
methods (Static)
function obj = loadobj(s)
if isstruct(s)
fh = s.FuncHandle;
rg = s.Range;
obj = GraphExpression(fh,rg);
else
makeGraph(s);
obj = s;
end
end
end
end
13-15
13 Saving and Loading Objects
h =
FuncHandle: @(x)x.^4
Range: [1x25 double]
save myFile h
close
load myFile h
If the load function cannot create the object and passes a struct to loadobj, loadobj attempts to
create an object with the data supplied.
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-16
Maintain Class Compatibility
Rename Property
Suppose that you want to rename a property, but do not want to cause errors in existing code that
refer to the original property. For example, rename a public property called OfficeNumber to
Location. Here is the original class definition:
classdef EmployeeList
properties
Name
Email
OfficeNumber % Rename as Location
end
end
• In the class definition, set the OfficeNumber property attributes to Dependent and Hidden.
• Create a property set method for OfficeNumber that sets the value of the Location property.
• Create a property get method for OfficeNumber that returns the value of the Location
property.
While the OfficeNumber property is hidden, existing code can continue to access this property. The
Hidden attribute does not affect access.
Because OfficeNumber is dependent, there is no redundancy in storage required by adding the new
property. MATLAB does not store or save dependent properties.
13-17
13 Saving and Loading Objects
end
end
You can load old instances of the EmployeeList class in the presence of the new class version. Code
that refers to the OfficeNumber property continues to work.
Suppose that you want to be able to load new EmployeeList objects into systems that still have the
old version of the EmployeeList class. To achieve compatibility with old and new versions:
In this version of the EmployeeList class, the OfficeNumber property saves the value used by the
Location property. Loading an object assigns values of the three original properties (Name, Email,
and OfficeNumber), but does not assign a value to the new Location property. The lack of the
Location property in the old class definition is not a problem.
classdef EmployeeList
properties
Name
Email
end
properties (Dependent)
Location
end
properties (Hidden)
OfficeNumber
end
methods
function obj = set.Location(obj,val)
obj.OfficeNumber = val;
end
function val = get.Location(obj)
val = obj.OfficeNumber;
end
end
end
Consider a class that has an AccountID property. Suppose that all account numbers must migrate
from eight-digit numeric values to 12-element character arrays.
• Tests to determine if the load function passed a struct or object. All loadobj methods must
handle both struct and object when there is an error in load.
13-18
Maintain Class Compatibility
• Tests to determine if the AccountID number contains eight digits. If so, change it to a 12-element
character array by calling the padAccID method.
After updating the AccountID property, loadobj returns a MyAccount object that MATLAB loads
into the workspace.
classdef MyAccount
properties
AccountID
end
methods
function obj = padAccID(obj)
ac = obj.AccountID;
acstr = num2str(ac);
if length(acstr) < 12
obj.AccountID = [acstr,repmat('0',1,12-length(acstr))];
end
end
end
methods (Static)
function obj = loadobj(a)
if isstruct(a)
obj = MyAccount;
obj.AccountID = a.AccountID;
obj = padAccID(obj);
elseif isa(a,'MyAccount')
obj = padAccID(a);
end
end
end
end
You do not need to implement a saveobj method. You are using loadobj only to ensure that older
saved objects are brought up to date while loading.
Suppose that you define a class to represent an entry in a phone book. The PhoneBookEntry class
defines three properties: Name, Address, and PhoneNumber.
classdef PhoneBookEntry
properties
Name
Address
PhoneNumber
end
end
However, in future releases, the class adds more properties. To provide flexibility, PhoneBookEntry
saves property data in a struct using its saveobj method.
methods
function s = saveobj(obj)
s.Name = obj.Name;
13-19
13 Saving and Loading Objects
s.Address = obj.Address;
s.PhoneNumber = obj.PhoneNumber;
end
end
The loadobj method creates the PhoneBookEntry object, which is then loaded into the workspace.
methods (Static)
function obj = loadobj(s)
if isstruct(s)
newObj = PhoneBookEntry;
newObj.Name = s.Name;
newObj.Address = s.Address;
newObj.PhoneNumber = s.PhoneNumber;
obj = newObj;
else
obj = s;
end
end
end
With these changes, you could not load a version 2 object in a previous release. However, version 2
employs several techniques to enable compatibility:
• Preserve the Address property (which is used in version 1) as a Dependent property with private
SetAccess.
• Define an Address property get method (get.Address) to build a char vector that is compatible
with the version 2 Address property.
• The saveobj method invokes the get.Address method to assign the object data to a struct
that is compatible with previous versions. The struct continues to have only an Address field
built from the data in the new StreetAddress, City, State, and ZipCode properties.
• When the loadobj method sets the Address property, it invokes the property set method
(set.Address), which extracts the substrings required by the StreetAddress, City, State,
and ZipCode properties.
• The Transient (not saved) property SaveInOldFormat enables you to specify whether to save
the version 2 object as a struct or an object.
classdef PhoneBookEntry
properties
Name
StreetAddress
City
State
ZipCode
PhoneNumber
end
properties (Constant)
Sep = ', '
end
13-20
Maintain Class Compatibility
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-21
13 Saving and Loading Objects
Enable ConstructOnLoad when you do not want to implement a loadobj method, but must
perform some actions at construction time. For example, enable ConstructOnLoad when you are
registering listeners for another object. Ensure that MATLAB can call the class constructor with no
arguments without generating an error.
Attributes set on superclasses are not inherited by subclasses. Therefore, MATLAB does not use the
value of the superclass ConstructOnLoad attribute when loading objects. If you want MATLAB to
call the class constructor, set the ConstructOnLoad attribute in your specific subclass.
The LabResults class shares the constructor object initialization steps with the loadobj method by
performing these steps in the assignStatus method.
classdef LabResult
properties
CurrentValue
end
properties (Transient)
Status
end
methods
function obj = LabResult(cv)
obj.CurrentValue = cv;
obj = assignStatus(obj);
end
function obj = assignStatus(obj)
v = obj.CurrentValue;
if v < 10
obj.Status = 'Too low';
13-22
Initialize Objects When Loading
The LabResults class uses loadobj to determine the status of a given test value. This approach
provides a way to:
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-23
13 Saving and Loading Objects
If the most specific class of an object does not define a loadobj or saveobj method, this class can
inherit loadobj or saveobj methods from a superclass.
• Superclasses implement saveobj methods to save their property data in the struct.
• The subclass saveobj method calls each superclass saveobj method and returns the completed
struct to the save function. Then the save function writes the struct to the MAT-file.
• The subclass loadobj method creates a subclass object and calls superclass methods to assign
their property values in the subclass object.
• The subclass loadobj method returns the reconstructed object to the load function, which loads
the object into the workspace.
The following superclass (MySuper) and subclass (MySub) definitions show how to code these
methods.
• The MySuper class defines a loadobj method to enable an object of this class to be loaded
directly.
• The subclass loadobj method calls a method named reload after it constructs the subclass
object.
• reload first calls the superclass reload method to assign superclass property values and then
assigns the subclass property value.
classdef MySuper
properties
X
Y
end
13-24
Save and Load Objects from Class Hierarchies
methods
function S = saveobj(obj)
S.PointX = obj.X;
S.PointY = obj.Y;
end
function obj = reload(obj,S)
obj.X = S.PointX;
obj.Y = S.PointY;
end
end
methods (Static)
function obj = loadobj(S)
if isstruct(s)
obj = MySuper;
obj = reload(obj,S);
end
end
end
end
Call the superclass saveobj and loadobj methods from the subclass saveobj and loadobj
methods.
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Object Save and Load”
13-25
13 Saving and Loading Objects
Restore Listeners
In this section...
“Create Listener with loadobj” on page 13-26
“Use Transient Property to Load Listener” on page 13-26
“Using the BankAccount and AccountManager Classes” on page 13-27
When the AccountBalance property value changes, the listener callback determines the account
status. Important points include:
• The BankAccount class defines the AccountManagerListener property to contain the listener
handle. This property enables the loadobj method to create a listener and return a reference to
it in the object that is loaded into the workspace.
• The AccountManagerListener property is Transient because there is no need to store the
listener handle with a BankAccount object. Create a listener that is attached to the new
BankAccount object created during the load process.
• The AccountBalance listener sets the AccountStatus.
• Only the AccountManager class can access AccountStatus property.
13-26
Restore Listeners
else
obj.AccountManagerListener = AccountManager.addAccount(obj);
end
end
end
end
Assume the AccountManager class provides services for various types of accounts. For the
BankAccount class, the AccountManager class defines two Static methods:
• assignStatus — Callback for the AccountBalance property PostSet listener. This method
determines the value of the BankAccount AccountStatus property.
• addAccount — Creates the AccountBalance property PostSet listener. The BankAccount
constructor and loadobj methods call this method.
classdef AccountManager
methods (Static)
function assignStatus(BA,~)
if BA.AccountBalance < 0 && BA.AccountBalance >= -100
BA.AccountStatus = 'overdrawn';
elseif BA.AccountBalance < -100
BA.AccountStatus = 'frozen';
else
BA.AccountStatus = 'open';
end
end
function lh = addAccount(BA)
lh = addlistener(BA,'AccountBalance','PostSet', ...
@(src,evt)AccountManager.assignStatus(BA));
end
end
end
ba = BankAccount(100)
ba =
AccountBalance: 100
AccountManagerListener: [1x1 event.proplistener]
AccountStatus: 'New Account'
Now set an account value to confirm that the AccountManager sets AccountStatus appropriately:
ba.AccountBalance = -10;
ba.AccountStatus
ans =
overdrawn
13-27
13 Saving and Loading Objects
See Also
Related Examples
• “Modify the Save and Load Process” on page 13-12
• “Property Attributes” on page 8-8
• “Listen for Changes to Property Values” on page 11-32
• “Object Save and Load”
13-28
14
Enumerations
Named Values
In this section...
“Kinds of Predefined Names” on page 14-2
“Techniques for Defining Enumerations” on page 14-2
• Constant properties
• Enumerations
Constant Properties
Use constant properties when you want a collection of related constant values that can belong to
different types (numeric values, character strings, and so on). Define properties with constant values
by setting the property Constant attribute. Reference constant properties by name whenever you
need access to that particular value.
See “Define Class Properties with Constant Values” on page 15-2 for more information.
Enumerations
Use enumerations when you want to create a fixed set of names representing a single type of value.
Use this new type in multiple places without redefining it for each class.
You can derive enumeration classes from other classes to inherit the operations of the superclass. For
example, if you define an enumeration class that subclasses a MATLAB numeric class like double or
int32, the enumeration class inherits all the mathematical and relational operations that MATLAB
defines for those classes.
Using enumerations instead of character strings to represent a value, such as colors ('red'), can
result in more readable code because:
14-2
Named Values
You can define enumeration classes in ways that are most useful to your application, as described in
the following sections.
Simple enumeration classes have no superclasses and no properties. These classes define a set of
related names that have no underlying values associated with them. Use this kind of enumeration
when you want descriptive names, but your application does not require specific information
associated with the name.
See the WeekDays class in the “Enumeration Class” on page 14-4 and the “Define Methods in
Enumeration Classes” on page 14-5 sections.
Enumeration classes that subclass MATLAB built-in classes inherit most of the behaviors of those
classes. For example, an enumeration class derived from the double class inherits the mathematical,
relational, and set operations that work with variables of the class.
Enumerations do not support the colon (:) operator, even if the superclass does.
Enumeration classes that do not subclass MATLAB built-in numeric and logical classes can define
properties. These classes can define constructors that set each member's unique property values.
The constructor can save input arguments in property values. For example, a Color class can specify
a Red enumeration member color with three (Red, Green, Blue) values:
enumeration
Red (1,0,0)
end
See Also
Related Examples
• “Enumeration Class Restrictions” on page 14-26
• “Enumerations Derived from Built-In Classes” on page 14-27
• “Enumerations That Encapsulate Data” on page 14-37
14-3
14 Enumerations
Enumeration Class
Create an enumeration class by adding an enumeration block to a class definition. For example, the
WeekDays class enumerates a set of days of the week.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
To execute the MATLAB code in the following sections, place the WeekDays class definition in a .m
file on your path.
ClassName.MemberName
For example, assign the enumeration member WeekDays.Tuesday to the variable today:
today = WeekDays.Tuesday;
whos
today
today =
Tuesday
14-4
Define Enumeration Classes
must be able to accept its own class as input and return an instance of the superclass. MATLAB built-
in numeric classes, such as uint32, allow this conversion.
For example, the Bearing class derives from the uint32 built-in class:
a = Bearing.East;
b = uint32(a);
whos
a 1x1 4 Bearing
b 1x1 4 uint32
The uint32 constructor accepts an object of the subclass Bearing and returns an object of class
uint32.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
methods
function tf = isMeetingDay(obj)
tf = WeekDays.Tuesday == obj;
end
end
end
today = WeekDays.Tuesday;
today.isMeetingDay
ans =
14-5
14 Enumerations
You can also use the enumeration member as a direct input to the method:
isMeetingDay(WeekDays.Wednesday)
ans =
classdef SyntaxColors
properties
R
G
B
end
methods
function c = SyntaxColors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Error (1, 0, 0)
Comment (0, 1, 0)
Keyword (0, 0, 1)
String (1, 0, 1)
end
end
When you refer to an enumeration member, the constructor initializes the property values:
e = SyntaxColors.Error;
e.R
ans =
Because SyntaxColors is a value class (it does not derive from handle), only the class constructor
can set property values:
e.R = 0
For more information on enumeration classes that define properties, see “Mutable Handle vs.
Immutable Value Enumeration Members” on page 14-32.
14-6
Define Enumeration Classes
MATLAB provides a default constructor for all enumeration classes that do not explicitly define a
constructor. The default constructor creates an instance of the enumeration class:
For example, the input arguments for the Bool class are 0 for Bool.No and 1 for Bool.Yes.
The values of 0 and 1 are of class logical because the default constructor passes the argument to
the first superclass. That is, this statement:
n = Bool.No;
MATLAB passes the member argument only to the first superclass. For example, suppose Bool
derived from another class:
classdef MyBool
methods
function boolValues = testBools(obj)
...
end
end
end
14-7
14 Enumerations
See Also
Related Examples
• “Refer to Enumerations” on page 14-9
• “Operations on Enumerations” on page 14-16
14-8
Refer to Enumerations
Refer to Enumerations
In this section...
“Instances of Enumeration Classes” on page 14-9
“Conversion of Characters to Enumerations” on page 14-10
“Enumeration Arrays” on page 14-12
The WeekDays class defines enumeration members for five days of the week.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
today = WeekDays.Monday;
tomorrow = WeekDays.Tuesday;
The variables today and tomorrow are objects of the WeekDays class.
The PPM class defines three enumeration members. Each member has an associated numeric value
derived from the class superclass.
level = PPM.High;
When you substitute enumeration members for instances of the superclass, MATLAB coerces the
enumeration member to the superclass. For example, add a numeric value to an enumeration member
of the PPM class.
levelNew =
1100
14-9
14 Enumerations
whos
You can substitute superclass values for corresponding enumeration members. For example, pass one
of the numeric values defined in the enumeration class to the PPMSwitch function.
function PPMSwitch(ppm)
switch ppm
case PPM.Low
disp Low
case PPM.Medium
disp Medium
case PPM.High
disp High
end
end
PPMSwitch(100)
Medium
Medium
For information on operations you can perform on enumeration class instances, see “Operations on
Enumerations” on page 14-16.
Use a char vector instead of a direct reference to an enumeration member when you want to use a
simple character string to specify an enumeration member. However, specifying an enumeration
member directly eliminates the conversion from char to enumeration.
Because the char vector 'Tuesday' matches the enumeration member WeekDays.Tuesday, the
Weekdays char method can perform the conversion.
class(today)
ans =
WeekDays
Create an enumeration array using the WeekDay class constructor and a cell array of char vectors.
14-10
Refer to Enumerations
wd = WeekDays({'Monday','Wednesday','Friday'})
wd =
class(wd)
ans =
WeekDays
All char vectors in the cell array must correspond to an enumeration member defined by the class.
MATLAB coerces char vectors into enumeration members when the dominant argument is an
enumeration. Because user-defined classes are dominant over the char class, MATLAB attempts to
convert the char vector to a member of the enumeration class.
Create an enumeration array. Then insert a char vector that represents an enumeration member into
the array.
a = [WeekDays.Monday,WeekDays.Wednesday,WeekDays.Friday]
a =
a(end+1) = 'Tuesday'
a =
class(a)
ans =
WeekDays
You can use enumeration members in place of char vectors in cases where functions require char
vectors. For example, this call to sprintf expects a char vector, designated by the %s format
specifier.
sprintf('Today is %s',WeekDays.Friday)
ans =
Today is Friday
The automatic conversion of enumeration classes to char enable you to use enumeration members in
this case.
14-11
14 Enumerations
Enumeration Arrays
Create enumeration arrays by:
wd = [WeekDays.Tuesday,WeekDays.Wednesday,WeekDays.Friday];
a(1) = WeekDays.Tuesday;
a(2) = WeekDays.Wednesday;
a(3) = WeekDays.Friday;
You can concatenate enumeration members and char vectors as long as the char vector represents
an enumeration member.
clear a
a = [WeekDays.Wednesday,'Friday'];
class(a)
ans =
WeekDays
clear a
a(1) = WeekDays.Wednesday;
a(2) = 'Friday';
class(a)
ans =
WeekDays
The default member of an enumeration class is the first enumeration member defined in the
enumeration block. For the WeekDays class, the default enumeration member is WeekDays.Monday.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
MATLAB allows assignment to any element of an array, even if the array variable does not previously
exist. To fill in unassigned array elements, MATLAB uses the default enumeration member.
14-12
Refer to Enumerations
clear a
a(5) = WeekDays.Tuesday;
MATLAB must initialize the values of array elements a(1:4) with the default enumeration member.
The result of the assignment to the fifth element of the array a is:
a =
See Also
Related Examples
• “Operations on Enumerations” on page 14-16
14-13
14 Enumerations
properties
PropName EnumerationClass
end
This syntax restricts values of PropName to members of the enumeration class EnumerationClass.
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
Use the WeekDays enumerations to restrict the allowed values of the Today property:
classdef Days
properties
Today WeekDays
end
end
d = Days;
d.Today = WeekDays.Tuesday;
d =
Today: Tuesday
The automatic conversion feature enables users of the Days class to assign values to the Today
property as either enumeration members, char vectors, or string scalars. The Today property is
14-14
Enumerations for Property Values
restricted to members of the WeekDays enumeration class. Therefore, you can assign a char vector
that represents a member of the WeekDays class.
d = Days;
d.Today = 'Tuesday';
d = Days;
d.Today = "Tuesday";
For more information on restricting property values, see “Validate Property Values” on page 8-18 and
“Property Class and Size Validation” on page 8-23.
14-15
14 Enumerations
Operations on Enumerations
In this section...
“Operations Supported by Enumerations” on page 14-16
“Example Enumeration Class” on page 14-16
“Default Methods” on page 14-16
“Convert Enumeration Members to Strings or char Vectors” on page 14-17
“Convert Enumeration Arrays to String Arrays or Cell Arrays of char Vectors” on page 14-17
“Relational Operations with Enumerations, Strings, and char Vectors” on page 14-18
“Enumerations in switch Statements” on page 14-19
“Enumeration Set Membership” on page 14-20
“Enumeration Text Comparison Methods” on page 14-21
“Get Information About Enumerations” on page 14-21
“Testing for an Enumeration” on page 14-22
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
For information on defining enumerations, see “Define Enumeration Classes” on page 14-4.
Default Methods
Enumeration classes have the following default methods:
methods('WeekDays')
Methods for class WeekDays:
The WeekDays method converts text formats into enumerations. Supported formats include strings,
char vectors, string arrays, and cell arrays of char vectors. For example:
f = WeekDays(["Monday" "Friday"])
14-16
Operations on Enumerations
f =
Monday Friday
ans =
ans =
'Today is Friday.'
sa = [WeekDays.Tuesday WeekDays.Thursday];
string(sa)
ans =
"Tuesday" "Thursday"
ca = cellstr([WeekDays.Tuesday WeekDays.Thursday]);
class(ca)
ans =
'cell'
class([ca{1:2}])
ans =
'char'
14-17
14 Enumerations
Use eq and ne to compare enumeration members with text values. For example, you can compare an
enumeration member with a string:
today = WeekDays.Friday;
today == "Friday"
ans =
logical
Compare an enumeration array to one char vector. The return value is a logical array indicating
which members of the enumeration array are equivalent to the char vector:
ans =
0 0 1
This example uses the ne function to compare the corresponding elements of an enumeration array
and a string array of equal length:
ans =
1 1 0
The char vector Wednesday is equal to (==) the enumeration member WeekDays.Wednesday. You
can use this equality in conditional statements:
today = 'Wednesday';
...
if today == WeekDays.Wednesday
disp('Team meeting at 2:00')
end
isequal Method
The isequal method also enables comparisons between enumeration instances and strings,
character vectors, string arrays, and cell arrays of character vectors.
14-18
Operations on Enumerations
a = WeekDays.Monday;
isequal(a,"Monday")
ans =
logical
When comparing an enumeration array to a single item, the behavior of isequal differs slightly from
eq and ne. The isequal method requires that the two values being compared are the same size.
Therefore, isequal returns false when comparing an enumeration array to a char vector or string
scalar, even if the text matches one of the enumeration members in the array.
ans =
logical
function c = Reminder(day)
% Add error checking here
switch(day)
case WeekDays.Monday
c = 'No meetings';
case WeekDays.Tuesday
c = 'Department meeting at 10:00';
case {WeekDays.Wednesday WeekDays.Friday}
c = 'Team meeting at 2:00';
case WeekDays.Thursday
c = 'Volleyball night';
end
end
today = WeekDays.Wednesday;
Reminder(today)
ans =
You can use strings or char vectors to represent specific enumeration members:
14-19
14 Enumerations
function c = Reminder2(day)
switch(day)
case 'Monday'
c = 'Department meeting at 10:00';
case 'Tuesday'
c = 'Meeting Free Day!';
case {'Wednesday' 'Friday'}
c = 'Team meeting at 2:00';
case 'Thursday'
c = 'Volleyball night';
end
end
Although you can use char vectors or strings instead of specifying enumerations explicitly, MATLAB
must convert the text format to an enumeration. Eliminate the need for this conversion if it is not
necessary.
Determine if today is a meeting day for your team. Create a set of enumeration members
corresponding to the days on which the team has meetings.
today = WeekDays.Tuesday;
teamMeetings = [WeekDays.Wednesday WeekDays.Friday];
ismember(today,teamMeetings)
ans =
0
If you pass both enumeration members and text values to an enumeration class method, the class
attempts to convert the text value to the class of the enumeration.
ans =
logical
14-20
Operations on Enumerations
ismember(WeekDays.Friday,["Wednesday" "Friday"])
ans =
logical
The string comparison methods can compare enumeration members with char vectors and strings.
today = WeekDays.Tuesday;
strcmp(today,'Friday')
ans =
strcmp(today,"Tuesday")
ans =
enumeration WeekDays
Monday
Tuesday
Wednesday
Thursday
Friday
For more information on how class introspection works with enumerations, see “Metaclass
EnumeratedValues Property” on page 16-7.
14-21
14 Enumerations
today = WeekDays.Wednesday;
isenum(today)
ans =
noday = WeekDays.empty;
isenum(noday)
ans =
today = WeekDays.Wednesday;
mc = metaclass(today);
mc.Enumeration
ans =
See Also
Related Examples
• “Enumeration Class Restrictions” on page 14-26
14-22
Hide Enumeration Members
When an enumeration class derives from another class, such as a numeric or logical class, then each
member can have a value associated with it. If two members have the same value assigned to them,
then the member defined first in the class definition masks the second member. Both names are valid
enumeration members, but the first one defined is the primary member. While masking makes it
possible to use one member name in place of another, it does not hide the secondary name from the
class users.
Using the Hidden attribute removes the masked member names from user view. For example, the
HighlightColor class defines enumeration members that represent syntax highlighting colors.
classdef HighlightColor < int32
enumeration
red (1)
green (2)
blue (3)
end
end
A new version of the class uses more descriptive member names, but the class needs to avoid
breaking existing code that uses the original member names, red, green, and blue. Using the Hidden
attribute for enumeration members enables the class to hide the original members.
classdef HighlightColor < int32
enumeration
error (1)
comment (2)
keyword (3)
end
enumeration (Hidden)
red (1)
green (2)
blue (3)
end
end
Code that uses the original member names continues to work. For example, existing references to the
now-hidden member HighlightColor.blue is compatible with the same-valued nonhidden member
HighlightColor.keyword.
a = HighlightColor.blue
a =
HighlightColor enumeration
keyword
a == HighlightColor.Keyword
ans =
14-23
14 Enumerations
logical
For enumeration members that represent values, the first member defined in the class is the primary
member for that value. For example, in the HighlightColor class, keyword is the primary member
and blue is the secondary member, both representing the value 3. Typically, the primary member is
not hidden while the secondary member is hidden. However, if the class design requires that the
primary member is hidden, then the secondary member must be hidden too.
For example, the PCComponents class defines enumerations that are used in an online form for a
computer order. While the FloppyDrive component is obsolete, the enumeration member can
remain in the class as a hidden member. The form can exclude FloppyDrive from the list of choices,
but the class author can keep this member available so that existing forms that refer to
FloppyDrive are still valid.
classdef PCComponents
enumeration
USBSlots
CDPlayer
end
enumeration (Hidden)
FloppyDrive
end
end
mc =?HighlightColor
mc =
Name: 'HighlightColor'
Description: ''
DetailedDescription: ''
Hidden: 0
Sealed: 0
Abstract: 0
Enumeration: 1
ConstructOnLoad: 0
HandleCompatible: 0
InferiorClasses: {[1×1 meta.class]}
14-24
Hide Enumeration Members
mc.EnumerationMemberList(4)
ans =
Name: 'red'
Description: ''
DetailedDescription: ''
Hidden: 1
To list the names of all hidden members, use the handle class findobj method:
findobj(mc.EnumerationMemberList,'Hidden',true).Name
ans =
'red'
ans =
'green'
ans =
'blue'
See Also
findobj | enumeration
14-25
14 Enumerations
• Enumeration classes are implicitly Sealed. You cannot define a subclass of an enumeration class
because doing so would expand the set.
• The properties of value-based enumeration classes are immutable. Only the constructor can assign
property values. MATLAB implicitly defines the SetAccess attributes of all properties defined by
value-based enumeration classes as immutable. You cannot set the SetAccess attribute to any
other value.
• All properties inherited by a value-based enumeration class that are not defined as Constant
must have immutable SetAccess.
• The properties of handle-based enumeration classes are mutable. You can set property values on
instances of the enumeration class. See “Mutable Handle vs. Immutable Value Enumeration
Members” on page 14-32.
• An enumeration member cannot have the same name as a property, method, or event defined by
the same class or inherited from a superclass. For example, an enumeration class that inherits
from a built-in numeric superclass inherits the full method from the superclass, so "full" is not a
valid member name.
• Enumerations do not support colon (a:b) operations. For example,
FlowRate.Low:FlowRate.High causes an error even if the FlowRate class derives from a
numeric superclass.
• Classes that define enumerations cannot restrict properties of the same class to an enumeration
type. Create a separate enumeration class to restrict property values to an enumeration. For
information on restricting property values, see “Example of Restricted Property” on page 14-14.
• If the primary enumeration member sets the Hidden attribute, then the secondary member (one
with the same underlying value) must also set the Hidden attribute. For more information, see
“Hide Enumeration Members” on page 14-23.
See Also
Related Examples
• “Enumerations Derived from Built-In Classes” on page 14-27
14-26
Enumerations Derived from Built-In Classes
For a more basic discussion of enumeration classes, see “Define Enumeration Classes” on page 14-4.
Note Enumeration classes derived from built-in numeric and logical classes cannot define properties.
If an enumeration class subclasses a built-in numeric class, the subclass inherits ordering and
arithmetic operations that you can apply to the enumerated names.
For example, the Results class subclasses the int32 built-in class. This class associates an integer
value with each of the four enumeration members — First, Second, Third, and NoPoints.
classdef Results < int32
enumeration
First (100)
Second (50)
Third (10)
NoPlace (0)
end
end
The enumeration member inherits the methods of the int32 class (except the colon operator). Use
these enumerations like numeric values (summed, sorted, averaged).
isa(Results.Second,'int32')
ans =
For example, use enumeration names instead of numbers to rank two teams:
Team1 = [Results.First, Results.NoPlace, Results.Third, Results.Second];
Team2 = [Results.Second, Results.Third, Results.First, Results.First];
14-27
14 Enumerations
ans =
160
mean(Team1)
ans =
40
sort(Team2,'descend')
ans =
ans =
1 0 0 0
ans =
When you first refer to an enumeration class that derives from a built-in class such as, int32,
MATLAB passes the input arguments associated with the enumeration members to the superclass
constructor. For example, referencing the Second Results member, defined as:
Second (50)
Specify aliased names with the same superclass constructor argument as the actual name:
classdef Bool < logical
enumeration
No (0)
Yes (1)
off (0)
14-28
Enumerations Derived from Built-In Classes
on (1)
end
end
For example, the actual name of an instance of the Bool.off enumeration member is No:
a = Bool.No
a =
No
b = Bool.off
b =
No
This class derives from the built-in logical class. Therefore, underlying values for an enumeration
member depend only on what value logical returns when passed that value:
a = Bool.Yes
a =
Yes
logical(a)
ans =
The FlowRate enumeration class defines three members, Low, Medium, and High.
classdef FlowRate < int32
enumeration
Low (10)
Medium (50)
High (100)
end
end
14-29
14 Enumerations
setFlow = FlowRate.Medium;
This statement causes MATLAB to call the default constructor with the argument value of 50.
MATLAB passes this argument to the first superclass constructor (int32(50) in this case). The
result is an underlying value of 50 as a 32-bit integer for the FlowRate.Medium member.
Because FlowRate subclasses a built-in numeric class (int32), this class cannot define properties.
However FlowRate inherits int32 methods including a converter method. Programs can use the
converter to obtain the underlying value:
setFlow = FlowRate.Medium;
int32(setFlow)
ans =
50
Default Converter
If an enumeration is a subclass of a built-in numeric class, you can convert from built-in numeric data
to the enumeration using the name of the enumeration class. For example:
a = Bool(1)
a =
Yes
An enumerated class also accepts enumeration members of its own class as input arguments:
Bool(a)
ans =
Yes
Bool([0,1])
ans =
No Yes
Bool.empty
ans =
14-30
Enumerations Derived from Built-In Classes
See Also
Related Examples
• “Mutable Handle vs. Immutable Value Enumeration Members” on page 14-32
• “Fundamental MATLAB Classes”
14-31
14 Enumerations
In this section...
“Select Handle- or Value-Based Enumerations” on page 14-32
“Value-Based Enumeration Classes” on page 14-32
“Handle-Based Enumeration Classes” on page 14-33
“Represent State with Enumerations” on page 14-35
A value-based enumeration class has a fixed set of specific values. Modify these values by changing
the values of properties. Doing so expands or changes the fixed set of values for this enumeration
class.
Value-based enumeration classes implicitly define the SetAccess attributes of all properties as
immutable. You cannot set the SetAccess attribute to any other value.
However, all superclass properties must explicitly define property SetAccess as immutable.
An instance of a value-based enumeration class is unique until the class is cleared and reloaded. For
example, given this class:
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
end
end
a = WeekDays.Monday;
b = WeekDays.Monday;
isequal(a,b)
ans =
a == b
14-32
Mutable Handle vs. Immutable Value Enumeration Members
ans =
Value-based enumeration classes that define properties are immutable. For example, the Colors
enumeration class associates RGB values with color names.
classdef Colors
properties
R = 0
G = 0
B = 0
end
methods
function c = Colors(r,g,b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Red (1, 0, 0)
Green (0, 1, 0)
Blue (0, 0, 1)
end
end
red = Colors.Red;
[red.R,red.G,red.B]
ans =
1 0 0
red.G = 1;
Note You cannot derive an enumeration class from matlab.mixin.Copyable because the number
of instances you can create are limited to the ones defined inside the enumeration block.
Given a handle-based enumeration class with properties, changing the property value of an instance
causes all references to that instance to reflect the changed value.
14-33
14 Enumerations
For example, the HandleColors enumeration class associates RGB values with color names, the
same as the Colors class in the previous example. However, HandleColors derives from handle:
classdef HandleColors < handle
properties
R = 0
G = 0
B = 0
end
methods
function c = HandleColors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Red (1, 0, 0)
Green (0, 1, 0)
Blue (0, 0, 1)
end
end
ans =
MATLAB constructs the HandleColors.Red enumeration member, which sets the R property to 1,
the G property to 0, and the B property to 0.
After setting the value of the R property to 0.8, create another instance, b, of HandleColors.Red:
b = HandleColors.Red;
b.R
ans =
0.8000
The value of the R property of the newly created instance is also 0.8. A MATLAB session has only one
value for any enumeration member at any given time.
Clearing the workspace variables does not change the current definition of the enumeration member
HandleColors.Red:
clear
a = HandleColors.Red;
a.R
ans =
0.8000
14-34
Mutable Handle vs. Immutable Value Enumeration Members
ans =
To prevent reassignment of a given property value, set that property's SetAccess attribute to
immutable.
ans =
The property values of a and b are the same, so isequal returns true. However, unlike handle
classes that are not enumeration classes, a and b are the same handle because there is only one
enumeration member. Determine handle equality using == (the handle eq method).
a == b
ans =
See the handle eq method for information on how isequal and == differ when used with handles.
The Machine class represents a machine with start and stop operations. The MachineState
enumerations are easy to work with because of their eq and char methods, and they result in code
that is easy to read.
classdef Machine < handle
properties (SetAccess = private)
14-35
14 Enumerations
State = MachineState.NotRunning
end
methods
function start(machine)
if machine.State == MachineState.NotRunning
machine.State = MachineState.Running;
end
disp (machine.State.char)
end
function stop(machine)
if machine.State == MachineState.Running
machine.State = MachineState.NotRunning;
end
disp (machine.State.char)
end
end
end
m = Machine;
m.start
Running
m.stop
NotRunning
See Also
Related Examples
• “Enumerations That Encapsulate Data” on page 14-37
14-36
Enumerations That Encapsulate Data
Note Enumeration classes that subclass built-in numeric or logical classes cannot define or inherit
properties. For more information on this kind of enumeration class, see “Enumerations Derived from
Built-In Classes” on page 14-27.
Define properties in an enumeration class if you want to associate specific data with enumeration
members, but do not need to inherit arithmetic, ordering, or other operations that MATLAB defines
for specific built-in classes.
Representing Colors
Define an enumeration class to represent the RGB values of the colors in a color set. The Colors
class defines names for the colors, each of which uses the RGB values as arguments to the class
constructor:
classdef Colors
properties
R = 0
G = 0
B = 0
end
methods
function c = Colors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Blueish (18/255,104/255,179/255)
Reddish (237/255,36/255,38/255)
Greenish (155/255,190/255,61/255)
Purplish (123/255,45/255,116/255)
Yellowish (1,199/255,0)
LightBlue (77/255,190/255,238/255)
end
end
You can access the property values via the enumeration member:
Colors.Reddish.R
14-37
14 Enumerations
ans =
0.9294
Suppose that you want to create a plot with the new shade of red named Reddish:
a = Colors.Reddish;
[a.R,a.G,a.B]
ans =
Use these values by accessing the enumeration member properties. For example, the myPlot
function accepts a Colors enumeration member as an input argument. The function accesses the
RGB values defining the color from the property values.
function h = myPlot(x,y,LineColor)
h = line('XData',x,'YData',y);
r = LineColor.R;
g = LineColor.G;
b = LineColor.B;
h.Color = [r g b];
end
h = myPlot(1:10,1:10,Colors.Reddish);
The Colors class encapsulates the definitions of a standard set of colors. You can change the
enumeration class definition of the colors and not affect functions that use the enumerations.
The Cars class defines categories used to inventory automobiles. The Cars class derives from the
CarPainter class, which derives from handle. The abstract CarPainter class defines a paint
method, which modifies the Color property when a car is painted another color.
The Cars class uses the Colors enumeration members to specify a finite set of available colors. The
exact definition of any given color can change independently of the Cars class.
14-38
Enumerations That Encapsulate Data
obj.MPG = mpg;
obj.Color = colr;
end
function paint(obj,colorobj)
if isa(colorobj,'Colors')
obj.Color = colorobj;
else
[~,cls] = enumeration('Colors');
disp('Not an available color')
disp(cls)
end
end
end
end
The CarPainter class requires its subclasses to define a method called paint:
c1 = Cars.Compact;
c1.Color
ans =
Greenish
c1.paint(Colors.Reddish)
c1.Color
ans =
Reddish
See Also
Related Examples
• “Save and Load Enumerations” on page 14-40
• “Enumerations for Property Values” on page 14-14
14-39
14 Enumerations
Basic Knowledge
See the save and load functions and “Save and Load Process for Objects” on page 13-2 for general
information on saving and loading objects.
To see a list of enumeration names defined by a class, use the enumeration function.
When loading these enumerations, MATLAB preserves names over underlying values. If the saved
named value is different from the current class definition, MATLAB uses the value defined in the
current class, and then issues a warning.
When loading these types of enumerations, MATLAB does not check the values associated with the
names in the current class definition. This behavior results from the fact that simple enumerations
have no underlying values and handle-based enumerations can legally have values that are different
than those values defined by the class.
If the changes to the enumeration class definition do not prevent MATLAB from loading the object
(that is, all the named values in the MAT-File are present in the modified class definition), then
MATLAB issues a warning that the class has changed and loads the enumeration.
In these five cases, MATLAB issues a warning. In case 1, there are no defined results. In cases 2
through 5, MATLAB loads as much of the saved data as possible as a struct:
14-40
Save and Load Enumerations
struct Fields
• ValueNames — A cell array of strings, one per unique value in the enumeration array.
• Values — An array of the same dimension as ValueNames containing the corresponding values
of the enumeration members named in ValueNames. Depending on the kind of enumeration class,
Values can be one of these:
• If the enumeration class derives from a built-in class, the array class is the same as the built-in
class. The values in the array are the underlying values of each enumeration member.
• Otherwise, a struct array representing the property name — property values pairs of each
enumeration member. For simple and handle-based enumerations, the struct array has no
fields.
• ValueIndices — a uint32 array of the same size as the original enumeration. Each element is
an index into the ValueNames and Values arrays. The content of ValueIndices represents the
value of each object in the original enumeration array.
See Also
More About
• “Named Values” on page 14-2
14-41
15
Constant Properties
15 Constant Properties
Use constant properties to define constant values that you can access by name. Create a class with
constant properties by declaring the Constant attribute in the property blocks. Setting the
Constant attribute means that, once initialized to the value specified in the property block, the value
cannot be changed.
Assign any value to a Constant property, including a MATLAB expression. For example:
classdef NamedConst
properties (Constant)
R = pi/180
D = 1/NamedConst.R
AccCode = '0145968740001110202NPQ'
RN = rand(5)
end
end
MATLAB evaluates the expressions when loading the class. Therefore, the values MATLAB assigns to
RN are the result of a single call to the rand function and do not change with subsequent references
to NamedConst.RN. Calling clear classes causes MATLAB to reload the class and reinitialize the
constant properties.
Refer to the constant using the class name and the property name:
ClassName.PropName
For example, to use the NamedConst class defined in the previous section, reference the constant for
the degree to radian conversion, R:
radi = 45*NamedConst.R
radi =
0.7854
15-2
Define Class Properties with Constant Values
Constants in Packages
To create a library for constant values that you can access by name, first create a package folder,
then define the various classes to organize the constants. For example, to implement a set of
constants that are useful for making astronomical calculations, define a AstroConstants class in a
package called constants:
+constants/@AstroConstants/AstroConstants.m
classdef AstroConstants
properties (Constant)
C = 2.99792458e8 % m/s
G = 6.67259 % m/kgs
Me = 5.976e24 % Earth mass (kg)
Re = 6.378e6 % Earth radius (m)
end
end
To use this set of constants, reference them with a fully qualified class name. For example, the
following function uses some of the constants defined in AstroContants:
function E = energyToOrbit(m,r)
E = constants.AstroConstants.G * constants.AstroConstants.Me * m * ...
(1/constants.AstroConstants.Re-0.5*r);
end
Importing the package into the function eliminates the need to repeat the package name (see
import):
function E = energyToOrbit(m,r)
import constants.*;
E = AstroConstants.G * AstroConstants.Me * m * ...
(1/AstroConstants.Re - 0.5 * r);
end
For example, the ConstMapClass class defines a constant property. The value of the constant
property is a handle object (a containers.Map object).
To assign the current date to the Date key, return the handle from the constant property, then make
the assignment using the local variable on the left side of the assignment statement:
localMap = ConstMapClass.ConstMapProp
localMap('Date') = datestr(clock);
15-3
15 Constant Properties
You cannot use a reference to a constant property on the left side of an assignment statement. For
example, MATLAB interprets the following statement as the creation of a struct named
ConstMapClass with a field ConstMapProp:
ConstMapClass.ConstMapProp('Date') = datestr(clock);
ans =
18-Apr-2002 09:56:59
Because MyProject is a handle class, you can get the handle to the instance that is assigned to the
constant property:
p = MyProject.ProjectInfo;
ans =
Engineering
Modify the nonconstant properties of the MyProject class using this handle:
p.Department = 'Quality Assurance';
p is a handle to the instance of MyProject that is assigned to the ProjectInfo constant property:
15-4
Define Class Properties with Constant Values
MyProject.ProjectInfo.Department
ans =
Quality Assurance
Clearing the class results in the assignment of a new instance of MyProject to the ProjectInfo
property.
clear MyProject
MyProject.ProjectInfo.Department
ans =
Engineering
You can assign an instance of the defining class as the default value of a property only when the
property is declared as Constant
See Also
Related Examples
• “Static Data” on page 4-2
More About
• “Named Values” on page 14-2
15-5
16
Class Metadata
In this section...
“What Is Class Metadata?” on page 16-2
“The meta Package” on page 16-2
“Metaclass Objects” on page 16-3
“Metaclass Object Lifecycle” on page 16-3
All class components have an associated metaclass, which you access from the meta.class object.
For example, create the meta.class object for the matlab.alias.AliasFileManager class:
mc = ?matlab.alias.AliasFileManager
mc =
Name: 'matlab.alias.AliasFileManager'
Description: 'AliasFileManager Create and edit alias definition files'
DetailedDescription: ' Use an AliasFileManager object to create and manage...
Hidden: 0
Sealed: 1
Abstract: 0
Enumeration: 0
ConstructOnLoad: 0
HandleCompatible: 1
InferiorClasses: {0×1 cell}
ContainingPackage: [1×1 meta.package]
Aliases: [0×1 string]
RestrictsSubclassing: 1
PropertyList: [1×1 meta.property]
MethodList: [28×1 meta.method]
EventList: [1×1 meta.event]
EnumerationMemberList: [0×1 meta.EnumeratedValue]
SuperclassList: [1×1 meta.class]
16-2
Class Metadata
Metaclass Objects
You cannot instantiate metaclasses directly by calling the respective class constructor. Create
metaclass objects from class instances or from the class name.
• ?ClassName — Returns a meta.class object for the named class. Use meta.class.fromName
with class names stored as characters in variables.
• meta.class.fromName('ClassName') — returns the meta.class object for the named class
(meta.class.fromName is a meta.class method).
• metaclass(obj) — Returns a metaclass object for the class instance (metaclass)
mc = ?MyClass;
Create meta.class object from class name using the fromName method:
mc = meta.class.fromName('MyClass');
obj = MyClass;
mc = metaclass(obj);
The metaclass function returns the meta.class object (that is, an object of the meta.class
class). You can obtain other metaclass objects (meta.property, meta.method, and so on) from the
meta.class object.
Note Metaclass is a term used here to refer to all the classes in the meta package. meta.class is a
class in the meta package whose instances contain information about MATLAB classes. Metadata is
information about classes contained in metaclasses.
However, MATLAB does not update existing metaclass objects to the new class definition. If you
change a class definition while metaclass objects of that class exist, MATLAB deletes the metaclass
objects and their handles become invalid. You must create a new metaclass object after updating the
class.
For information on how to modify and reload classes, see “Automatic Updates for Modified Classes”
on page 5-27.
16-3
16 Information from Class Metadata
See Also
Related Examples
• “Class Introspection with Metadata” on page 16-5
• “Find Objects with Specific Values” on page 16-9
• “Get Information About Properties” on page 16-12
• “Find Default Values in Property Metadata” on page 16-17
16-4
Class Introspection with Metadata
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
16-5
16 Information from Class Metadata
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.
a(1).Name
mc.SuperclassList(1).Name
ans =
handle
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
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.
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
16-6
Class Introspection with Metadata
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
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)
mpArray(2).GetAccess
ans =
private
mpArray(1).SetMethod
ans =
@D:\MyDir\@EmployeeData\EmployeeData.m>EmployeeData.set.EmployeeName
classdef WeekDays
enumeration
Monday, Tuesday, Wednesday, Thursday, Friday
16-7
16 Information from Class Metadata
end
end
mc = ?WeekDays;
mc.EnumerationMemberList(2).Name
ans =
Tuesday
See Also
Related Examples
• “Find Objects with Specific Values” on page 16-9
16-8
Find Objects with Specific Values
PB(2).addprop('HighSpeedInternet');
PB(2).HighSpeedInternet = '1M';
Find the object representing employee Nancy Wong and display the name and number by
concatenating the strings:
NW = findobj(PB,'Name','Nancy Wong');
[NW.Name,' - ',NW.Number]
ans =
Search for objects with specific property names using the -property option:
H = findobj(PB,'-property','HighSpeedInternet');
H.HighSpeedInternet
16-9
16 Information from Class Metadata
ans =
1M
The -property option enables you to omit the value of the property and search for objects using
only the property name.
ans =
5081234568
For example, find the abstract methods in a class definition by searching the meta.class
MethodList for meta.method objects with their Abstract property set to true:
Use the class name in character format because class is abstract. You cannot create an object of the
class:
mc = meta.class.fromName('MyClass');
Search the MethodList list of meta.method objects for those methods that have their Abstract
property set to true:
absMethods = findobj(mc.MethodList,'Abstract',true);
methodNames = {absMethods.Name};
The cell array, methodNames, contains the names of the abstract methods in the class.
Find the names of all properties in the containers.Map class that have public GetAccess:
mc = ?containers.Map;
mpArray = findobj(mc.PropertyList,'GetAccess','public');
names = {mpArray.Name};
Display the names of all containers.Map properties that have public GetAccess:
celldisp(names)
names{1} =
16-10
Find Objects with Specific Values
Count
names{2} =
KeyType
names{3} =
ValueType
~isempty(findobj([mc.MethodList(:)],'Static',true))
ans =
findobj returns an array of meta.method objects for the static methods. In this case, the list of
static methods is not empty. Therefore, there are static methods defined by this class.
Get the names of any static methods from the meta.method array:
staticMethodInfo = findobj([mc.MethodList(:)],'Static',true);
staticMethodInfo(:).Name
ans =
empty
The name of the static method (there is only one in this case) is empty. Here is the information from
the meta.method object for the empty method:
staticMethodInfo
Name: 'empty'
Description: 'Returns an empty object array of the given size'
DetailedDescription: ''
Access: 'public'
Static: 1
Abstract: 0
Sealed: 0
Hidden: 1
InputNames: {'varargin'}
OutputNames: {'E'}
DefiningClass: [1x1 meta.class]
See Also
empty
Related Examples
• “Get Information About Properties” on page 16-12
16-11
16 Information from Class Metadata
You can get the meta.property object for a property from the meta.class object. To get the
meta.class object for a class:
Create the meta.class object using the ? operator with the class name:
mc = ?BasicHandle
mc =
Name: 'BasicHandle'
Description: 'BasicHandle Inherits from handle superclass'
DetailedDescription: ' Defines 1 public and 2 private properties.'
Hidden: 0
Sealed: 0
Abstract: 0
Enumeration: 0
ConstructOnLoad: 0
HandleCompatible: 1
InferiorClasses: {0×1 cell}
ContainingPackage: [0×0 meta.package]
Aliases: [0×1 string]
RestrictsSubclassing: 0
16-12
Get Information About Properties
mc.PropertyList(1).Name
ans =
Date
The meta.class object contains a meta.property object for all properties, including hidden
properties. The properties function returns only public properties.
For a handle class, use the handle findprop method to get the meta.property object for a
specific property.
For example, find the meta.property object for the Category property of the BasicHandle class.
h = BasicHandle;
mp = findprop(h,'Category')
mp =
Name: 'Category'
Description: ''
DetailedDescription: ''
GetAccess: 'public'
SetAccess: 'public'
Dependent: 0
Constant: 0
Abstract: 0
Transient: 0
Hidden: 0
GetObservable: 0
SetObservable: 0
AbortSet: 0
NonCopyable: 0
GetMethod: []
SetMethod: []
HasDefault: 1
DefaultValue: 'new'
DefiningClass: [1×1 meta.class]
The meta.property display shows that a default BasicHandle object Category property:
For a list of property attributes, see “Table of Property Attributes” on page 8-8.
16-13
16 Information from Class Metadata
Access other metaclass objects directly from the meta.class object properties. For example, the
statement:
mc = ?containers.Map;
class(mc)
ans =
meta.class
Referencing the PropertyList meta.class property returns an array with one meta.property
object for each property of the containers.Map class:
class(mc.PropertyList)
ans =
meta.property
mc.Properties(1)
ans =
[1x1 meta.property]
The Name property of the meta.property object contains a char vector that is the name of the
property:
class(mc.PropertyList(1).Name)
ans =
char
mc.PropertyList(1).Name([1 end])
ans =
Ct
16-14
Get Information About Properties
• Find objects that define constant properties (Constant attribute set to true).
• Determine what properties are read-only (GetAccess = public, SetAccess = private).
The findAttrValue function returns a cell array of property names that set the specified attribute.
The function accesses information from metadata using these techniques:
• If input argument, obj, is a char vector, use the meta.class.fromName static method to get the
meta.class object.
• If input argument, obj, is an object, use the metaclass function to get the meta.class object.
• Every property has an associated meta.property object. Obtain these objects from the
meta.class PropertyList property.
• Use the handle class findprop method to determine if the requested property attribute is a
valid attribute name. All property attributes are properties of the meta.property object. The
statement, findobj(mp,'PropertyName') determines whether the meta.property object,
mp, has a property called PropertyName.
• Reference meta.property object properties using dynamic field names. For example, if
attrName = 'Constant', then MATLAB converts the expression mp.(attrName) to
mp.Constant
• The optional third argument enables you to specify the value of attributes whose values are not
logical true or false (such as GetAccess and SetAccess).
mapobj = containers.Map({'rose','bicycle'},{'flower','machine'});
findAttrValue(mapobj,'SetAccess','private')
16-15
16 Information from Class Metadata
ans =
findAttrValue(mapobj,'GetAccess','public')
ans =
See Also
Related Examples
• “Find Default Values in Property Metadata” on page 16-17
16-16
Find Default Values in Property Metadata
Default Values
Class definitions can specify explicit default values for properties (see “Define Properties with Default
Values” on page 8-13). Determine if a class defines an explicit default value for a property and what
the value of the default is from the property meta.property object.
meta.property Data
The meta.class object for a class contains a meta.property object for every property defined by
the class, including properties with private and protected access.
For example, get the meta.class object for the PropertyWithDefault class shown here:
classdef PropertyWithDefault
properties
Date = date
RandNumber = randi(9)
end
end
The second element in the mp array is the meta.property object for the RandNumber property.
Listing the meta.property object shows the information contained in its properties:
mp(2)
Name: 'RandNumber'
Description: ''
DetailedDescription: ''
GetAccess: 'public'
SetAccess: 'public'
Dependent: 0
Constant: 0
Abstract: 0
Transient: 0
Hidden: 0
GetObservable: 0
SetObservable: 0
AbortSet: 0
NonCopyable: 0
GetMethod: []
SetMethod: []
16-17
16 Information from Class Metadata
HasDefault: 1
DefaultValue: 5
DefiningClass: [1×1 meta.class]
• HasDefault — true (displayed as 1) if the class specifies a default value for the property, false
if it does not.
• DefaultValue — Contains the default value, when the class defines a default value for the
property. If the default value is an expression, the value of DefaultValue is the result of
evaluating the expression.
For more information on the evaluation of property default values defined by expressions, see
“Evaluation of Expressions in Class Definitions” on page 6-9.
These properties provide a programmatic way to obtain property default values without opening class
definition files. Use these meta.property object properties to obtain property default values for
both built-in classes and classes defined in MATLAB code.
Use the ? operator, the metaclass function, or the meta.class.fromName static method (works
with char vector variable) to obtain a meta.class object.
The meta.class object PropertyList contains an array of meta.property objects. Identify which
property corresponds to which meta.property object using the meta.property Name property.
Follow these steps to obtain the default value defined for the Material property. Include any error
checking that is necessary for your application.
16-18
Find Default Values in Property Metadata
for k = 1:length(mp)
if (strcmp(mp(k).Name,'Material')
...
4 Before querying the default value of the Material property, test the HasDefault
meta.property to determine if MyClass defines a default property for this property:
if mp(k).HasDefault
dv = mp(k).DefaultValue;
end
The DefaultValue property is read-only. Changing the default value in the class definition changes
the value of DefaultValue property. You can query the default value of a property regardless of its
access settings.
Abstract and dynamic properties cannot define default values. Therefore, MATLAB returns an error if
you attempt to query the default value of properties with these attributes. Always test the logical
value of the meta.property HasDefault property before querying the DefaultValue property to
avoid generating an error.
Class definitions can define property default values as MATLAB expressions (see “Evaluation of
Expressions in Class Definitions” on page 6-9 for more information). MATLAB evaluates these
expressions the first time the default value is needed, such as the first time you create an instance of
the class.
Querying the meta.property DefaultValue property causes MATLAB to evaluate a default value
expression, if it had not yet been evaluated. Therefore, querying a property default value can return
an error or warning if errors or warnings occur when MATLAB evaluates the expression. See
“Property with Expression That Errors” on page 16-20 for an example.
MyClass does not explicitly define a default value for the Foo property:
classdef MyFoo
properties
Foo
end
end
The meta.property instance for property Foo has a value of false for HasDefault. Because the
class does not explicitly define a default value for Foo, attempting to access the DefaultValue
property causes an error:
mc = ?MyFoo;
mp = mc.PropertyList(1);
mp.HasDefault
ans =
dv = mp.DefaultValue;
16-19
16 Information from Class Metadata
Abstract Property
classdef MyAbst
properties (Abstract)
Foo
end
end
The meta.property instance for property Foo has a value of false for its HasDefault property
because you cannot define a default value for an Abstract property. Attempting to access
DefaultValue causes an error:
mc = ?MyAbst;
mp = mc.PropertyList(1);
mp.HasDefault
ans =
dv = mp.DefaultValue;
MyPropEr defines the Foo property default value as an expression that errors when evaluated.
classdef MyPropEr
properties
Foo = sin(pie/2)
end
end
The meta.property object for property Foo has a value of true for its HasDefault property
because Foo does have a default value:
sin(pie/2)
However, this expression returns an error (pie is a function that creates a pie graph, not the value
pi).
mc = ?MyPropEr;
mp = mc.PropertyList(1);
mp.HasDefault
ans =
dv = mp.DefaultValue;
Querying the default value causes the evaluation of the expression and returns the error.
16-20
Find Default Values in Property Metadata
classdef MyEmptyProp
properties
Foo = []
end
end
The meta.property object for property Foo has a value of true for its HasDefault property.
Accessing DefaultValue returns the value []:
mc = ?MyEmptyProp;
mp = mc.PropertyList(1);
mp.HasDefault
ans =
dv = mp.DefaultValue;
dv =
[]
See Also
Related Examples
• “Get Information About Properties” on page 16-12
16-21
17
In this section...
“How to Customize Class Behavior” on page 17-2
“Which Methods Control Which Behaviors” on page 17-2
“Overload Functions and Override Methods” on page 17-3
You can change how user-defined objects behave by defining methods that control specific behaviors.
To change a behavior, implement the appropriate method with the name and signature of the
MATLAB function.
17-2
Methods That Modify Default Behavior
Overloading
Overloading means that there is more than one function or method having the same name within the
same scope. MATLAB dispatches to a particular function or method based on the dominant argument.
For example, the timeseries class overloads the MATLAB plot function. When you call plot with
a timeseries object as an input argument, MATLAB calls the timeseries class method named
plot.
Overriding
Overriding means redefining a method inherited from a superclass. MATLAB dispatches to the most
specific version of the method. That is, if the dominant argument is an object of the subclass, then
MATLAB calls the subclass method.
See Also
Related Examples
• “Overload Functions in Class Definitions” on page 9-25
• “Method Invocation” on page 9-11
• “Operator Overloading” on page 17-19
17-3
17 Specialize Object Behavior
Concatenation Methods
In this section...
“Default Concatenation” on page 17-4
“Methods to Overload” on page 17-4
Default Concatenation
You can concatenate objects into arrays. For example, suppose that you have three instances of the
class MyClass, obj1, obj2, obj3. You can form arrays of these objects using brackets. Horizontal
concatenation calls horzcat:
HorArray = [obj1,obj2,obj3];
HorArray is a 1-by-3 array of class MyClass. You can concatenate the objects along the vertical
dimension, which calls vertcat:
VertArray = [obj1;obj2;obj3]
VertArray is a 3-by-1 array of class MyClass. To concatenate arrays along different dimensions, use
the cat function. For example:
ndArray = cat(3,HorArray,HorArray);
Methods to Overload
Overload horzcat, vertcat, and cat to produce specialized behaviors in your class. Overload both
horzcat and vertcat whenever you want to modify object concatenation because MATLAB uses
both functions for any concatenation operation.
See Also
Related Examples
• “Subclasses of Built-In Types with Properties” on page 12-53
17-4
Object Converters
Object Converters
In this section...
“Why Implement Converters” on page 17-5
“Converters for Package Classes” on page 17-5
“Converters and Subscripted Assignment” on page 17-6
“Converter for Heterogeneous Arrays” on page 17-6
Suppose that you define a polynomial class. If you create a double method for the polynomial
class, you can use it to call other functions that require inputs of type double.
p = polynomial(...);
dp = double(p);
roots(dp)
p is a polynomial object, double is a method of the polynomial class, and roots is a standard
MATLAB function whose input arguments are the coefficients of a polynomial.
classdef MyClass
...
methods
function objPkgClass = PkgName.PkgClass(objMyclass)
...
end
end
end
You cannot define a converter method that uses dots in the name in a separate file. Define package-
class converters in the classdef file.
17-5
17 Specialize Object Behavior
A(1) = myobj;
MATLAB compares the class of the Right-Side variable to the class of the Left-Side variable. If the
classes are different, MATLAB attempts to convert the Right-Side variable to the class of the Left-Side
variable. To do this conversion, MATLAB first searches for a method of the Right-Side class that has
the same name as the Left-Side class. Such a method is a converter method, which is similar to a
typecast operation in other languages.
If the Right-Side class does not define a method to convert from the Right-Side class to the Left-Side
class, MATLAB calls the Left-Side class constructor. passing it the Right-Side variable.
A(1) = objA;
A(2) = objB;
MATLAB attempts to call a method of ClassB named ClassA. If no such converter method exists,
MATLAB software calls the ClassA constructor, passing objB as an argument. If the ClassA
constructor cannot accept objB as an argument, then MATLAB returns an error.
See Also
Related Examples
• “Converter Methods” on page 10-15
• “The Double Converter” on page 19-10
17-6
Customize Object Indexing
Arrays enable you to reference and assign elements of the array using a subscripted notation. This
notation specifies the indices of specific array elements. For example, suppose that you create two
arrays of numbers (using randi and concatenation).
A = randi(9,3,4)
A =
4 8 5 7
4 2 6 3
7 5 7 7
B = [3 6 9];
Reference and assign elements of either array using index values in parentheses:
B(2) = A(3,4);
B
B =
3 7 9
The MATLAB default behavior also works with user-defined objects. For example, create an array of
objects of the same class:
for k=1:3
objArray(k) = MyClass;
end
Referencing the second element in the object array, objArray, returns the object constructed when
k = 2:
D = objArray(2);
class(D)
ans =
MyClass
You can assign an object to an array of objects of the same class, or an uninitialized variable:
17-7
17 Specialize Object Behavior
newArray(3,4) = D;
Arrays of objects behave much like numeric arrays in MATLAB. You do not need to implement any
special methods to provide standard array behavior with your class.
To modify indexing behavior for your class, inherit from one or more modular indexing mixin classes.
Each class is responsible for one group of indexing operations:
Each class defines abstract methods that handle the details of each indexing operation the class
defines. Implement these methods to perform the operations your design requires.
You can inherit from these classes independently. For example, you can customize only parentheses
indexing by inheriting only from RedefinesParen. The dot and brace indexing behaviors in that
case are the default MATLAB behaviors.
You can also choose to customize just one or two levels of indexing and forward additional operations
to another MATLAB object. For example, you can author a class that customizes parentheses indexing
(using RedefinesParen) but uses the default behavior for dot method calls:
myInstance(2,1).value
See Also
Related Examples
• “Customize Parentheses Indexing for Mapping Class” on page 17-23
17-8
Code Patterns for subsref and subsasgn Methods
For R2021b and later, the recommended process for customizing indexing is to use the modular
indexing superclasses instead of overloading subsref and subsasgn. For more information, see
“Customize Object Indexing” on page 17-7.
If your class design requires that indexing operations return or assign a different number of values
than the number defined by the default indexing operation, overload the
numArgumentsFromSubscript function to control nargout for subsref and nargin for
subsasgn. For more information and examples, see numArgumentsFromSubscript.
For example, the CustomIndex class defines a property that you can use in indexing expressions.
17-9
17 Specialize Object Behavior
classdef CustomIndex
properties
DataArray
end
end
Create an object and assign a 5-by-5 matrix created by the magic function to the DataArray
property.
a = CustomIndex;
a.DataArray = magic(5);
This subscripted reference expression returns the first row of the 5-by-5 matrix.
a.DataArray(1,:)
ans =
17 24 1 8 15
This expression assigns new values to the first row of the array stored in the DataArray property.
a.DataArray(1,:) = [1 2 3 4 5];
The indexing structure contains this information in the type and subs fields.
• type — One of the three possible indexing types: '.', '()', '{}'
• subs — A char vector with the property name or cell array of the indices used in the expression,
including : and end.
If the indexing expression is a compound expression, then MATLAB passes an array of structures, one
struct for each level of indexing. For example, in this expression:
a.DataArray(1,:)
• S(1).type is set to '.', indicating that the first indexing operation is a dot.
• s(1).subs is set to the property name, 'DataArray'
The second level of indexing is in the second element of the indexing structure:
• S(2).types is set to '()' indicating the second indexing operation is parentheses indexing
17-10
Code Patterns for subsref and subsasgn Methods
• Determine the full indexing expression using the types and subs fields of the indexing structure.
• Implement the specialized behaviors for the indexing operations supported by the class.
• Return the appropriate values or modified objects in response to the call by MATLAB.
A switch statement is a convenient way to detect the first level of indexing. There are three types of
indexing—dot, parentheses, and braces. Each case block in the switch statement implements all
indexing expressions that begin with that first-level type of indexing.
The methods must implement all indexing expressions that the class supports. If you do not customize
a particular type of indexing, call the built-in function to handle that expression.
Use the length of the indexing structure array and indexing type define conditional statements for
compound indexing expressions.
The following framework for the subsref method shows how to use information in the indexing
structure in conditional statements. Your application can involve other expressions not shown here.
function varargout = subsref(obj,s)
switch s(1).type
case '.'
if length(s) == 1
% Implement obj.PropertyName
...
elseif length(s) == 2 && strcmp(s(2).type,'()')
% Implement obj.PropertyName(indices)
...
else
[varargout{1:nargout}] = builtin('subsref',obj,s);
end
case '()'
if length(s) == 1
% Implement obj(indices)
...
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj(ind).PropertyName
...
elseif length(s) == 3 && strcmp(s(2).type,'.') && strcmp(s(3).type,'()')
% Implement obj(indices).PropertyName(indices)
...
else
% Use built-in for any other expression
[varargout{1:nargout}] = builtin('subsref',obj,s);
end
case '{}'
if length(s) == 1
% Implement obj{indices}
...
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj{indices}.PropertyName
...
else
% Use built-in for any other expression
[varargout{1:nargout}] = builtin('subsref',obj,s);
end
otherwise
17-11
17 Specialize Object Behavior
Using varargout for the returned value enables the method to work with object arrays. For
example, suppose that you want to support the return of a comma-separated list with an expression
like this one:
[x1,...xn] = objArray.PropertyName(Indices)
This expression results in a two-element indexing structure array. The first-level type is dot ('.') and
the second level is parentheses ('()'). Build the varargout cell array with each value in the array.
case '.'
...
if length(s)==2 && strcmp(s(2).type,'()')
prop = s(1).subs; % Property name
n = numel(obj); % Number of elements in array
varargout = cell(1,n); % Preallocate cell array
for k = 1:n
varargout{k} = obj(k).(prop).(s(2).subs);
end
end
...
end
subsasgn Pattern
The following framework for the subsasgn method shows how to use the indexing structure in
conditional statements that implement assignment operations.
function obj = subsasgn(obj,s,varargin)
switch s(1).type
case '.'
if length(s) == 1
% Implement obj.PropertyName = varargin{:};
...
elseif length(s) == 2 && strcmp(s(2).type,'()')
% Implement obj.PropertyName(indices) = varargin{:};
...
else
% Call built-in for any other case
obj = builtin('subsasgn',obj,s,varargin{:});
end
case '()'
if length(s) == 1
% Implement obj(indices) = varargin{:};
elseif length(s) == 2 && strcmp(s(2).type,'.')
% Implement obj(indices).PropertyName = varargin{:};
...
elseif length(s) == 3 && strcmp(s(2).type,'.') && strcmp(s(3).type,'()')
% Implement obj(indices).PropertyName(indices) = varargin{:};
...
else
% Use built-in for any other expression
obj = builtin('subsasgn',obj,s,varargin{:});
end
case '{}'
if length(s) == 1
% Implement obj{indices} = varargin{:}
...
17-12
Code Patterns for subsref and subsasgn Methods
Using varargin for the right-side value of the assignment statement enables the method to work
with object arrays. For example, suppose that you want to support the assignment of a comma-
separated list with an expression like this one:
C = {'one';'two';'three'};
[objArray.PropertyName] = C{:}
This expression results in an indexing structure with the dot type ('.') indexing The cell array C on
the right side of the assignment statement produces a comma-separated list. This code assigns one
list item to each property in the object array.
case '.'
if length(s)==1
prop = s(1).subs; % Property name
n = numel(obj); % Number of elements in array
for k = 1:n
obj(k).(prop) = varargin{k};
end
end
end
Assigning to an element of an uninitialized variable results in a call to the subsasgn method of the
class on the right side of the assignment. For example, this class defines a subsasgn method that
simply calls the built-in subsasgn method for parenthesis indexing.
classdef MyClass
methods
function obj = subsasgn(obj,s,varargin)
switch s(1).type
case '()'
obj = builtin('subsasgn',obj,s,varargin{:});
end
end
end
end
When attempting to assign an object of MyClass to the first element of the uninitialized variable,
B(1) in the following statement, MATLAB calls the subsasgn method of MyClass with an empty
double ([]) as the first argument. The assignment can cause an error because the subsasgn method
must be passed an object of the class.
clear B
B(1) = MyClass;
17-13
17 Specialize Object Behavior
The subsasgn method can detect this situation and take the appropriate action, such as returning a
useful error message if the class does not support this type of assignment, or converting the input to
an object of the class and passing it to subsasgn.
For example, because MyClass can allow subscripted assignment to an uninitialized variable, the
subsasgn method can change the first argument from the empty double to an empty MyClass
object.
Use the isequal function to check the input and the empty static method to create the empty object.
classdef MyClass
methods
function obj = subsasgn(obj,s,varargin)
if isequal(obj,[])
obj = MyClass.empty;
end
obj = builtin('subsasgn',obj,s,varargin{:});
end
end
end
clear B
B(1) = MyClass;
B =
See Also
Related Examples
• “Subclasses of Built-In Types with Properties” on page 12-53
17-14
Overload end for Classes
In a standard MATLAB indexing expression, end returns the index value of the last element in the
dimension in which end appears. For example, in A(4,end), the end method returns the index of the
last element in the second dimension of A. You can overload end in classes for specialized behavior.
ind = end(A,k,n)
Note You cannot call the end method directly using this syntax. MATLAB automatically calls the
method when it encounters end in an indexing expression.
For example, A is a 2-by-3 array of doubles. When MATLAB encounters the expression A(end,1), it
calls the end method with these arguments.
end(A,1,2)
• A is the object.
• k = 1 because end appears in the first dimension of the indexing expression.
• n = 2 because the expression has two indices.
The end method returns 2, which is the index of the last element in the first dimension of A.
17-15
17 Specialize Object Behavior
The if-else statement calculates the return value based on where the end appears in the indexing
expression and whether the indexing expression has values for all of the dimensions of the object
array. For example, when B is a 2-by-3-by-2 object array of a type that inherits from
RedefinesParen:
• k < n: When end is not the last value in the indexing expression, the overload returns the last
value in that dimension. For B(1,end,4), end returns the size of the second dimension, 3.
• k = n: When end is the last element in the indexing expression, the overload handles two cases:
• If the indexing expression references all the indices, then prod(sz(k:end)) gives the same
result as sz(k). For example, in B(1,2,end), end returns 2.
• If the indexing expression does not reference all the indices, then prod(sz(k:end)) returns
the product of the size of dimension k and the sizes of all unreferenced dimensions. For
example, in B(1,end), end returns the product of the sizes of the second and third
dimensions, 6.
RedefinesParen defines size as an abstract method for the class author to implement, so the two
methods are dependent on one another for the final behavior. See the “Customize Parentheses
Indexing” example for a class that implements a size method that provides the expected end
behavior with an array.
See Also
Related Examples
• “Objects in Index Expressions” on page 17-17
17-16
Objects in Index Expressions
Objects as Indexes
MATLAB can use objects as indices in indexed expressions. The rules of array indexing apply —
indices must be positive integers. Therefore, MATLAB must be able to derive a value from the object
that is a positive integer for use in the indexed expression.
Indexed expressions like X(A), where A is an object, cause MATLAB to call the subsindex function.
However, if an indexing expression results in a call to an overloaded method from
matlab.mixin.indexing.RedefinesParen, matlab.mixin.indexing.RedefinesDot, or
matlab.mixin.indexing.RedefinesBrace defined by class X, then MATLAB does not call
subsindex.
• Define a subsindex method in the class of A that converts A to an integer. MATLAB calls A's
subsindex method to perform indexing operations when the class of X does not overload
methods from matlab.mixin.indexing.RedefinesParen,
matlab.mixin.indexing.RedefinesDot, or matlab.mixin.indexing.RedefinesBrace.
• If the class of X overloads methods from RedefinesParen, RedefinesDot, or RedefinesBrace
these methods can call the subsindex method of A explicitly. The class of A must implement a
subsindex method that returns an appropriate value.
subsindex Implementation
subsindex must return the value of the object as a zero-based integer index value in the range 0 to
prod(size(X))-1.
Suppose that you want to use object A to index into object B. B can be a single object or an array,
depending on the class designs.
C = B(A);
Here are two examples of subsindex methods. The first assumes you can convert class A to a
uint8. The second assumes class A stores an index value in a property.
• The subsindex method implemented by class A can convert the object to numeric format to be
used as an index:
17-17
17 Specialize Object Behavior
The class of obj implements a uint8 method to provide the conversion from the object to an
integer value.
• Class A implements subsindex to return a numeric value that is stored in a property:
See Also
matlab.mixin.indexing.RedefinesParen | matlab.mixin.indexing.RedefinesDot |
matlab.mixin.indexing.RedefinesBrace
Related Examples
• “Overload end for Classes” on page 17-15
17-18
Operator Overloading
Operator Overloading
In this section...
“Why Overload Operators” on page 17-19
“How to Define Operators” on page 17-19
“Sample Implementation — Addable Objects” on page 17-20
“MATLAB Operators and Associated Functions” on page 17-21
Each operator has an associated function (e.g., the + operator has an associated plus.m function).
You can implement any operator by creating a class method with the appropriate name. This method
can perform whatever steps are appropriate for the operation being implemented.
For a list of operators and associated function names, see “MATLAB Operators and Associated
Functions” on page 17-21.
User-defined classes have a higher precedence than built-in classes. For example, suppose q is an
object of class double and p is a user-defined class. Both of these expressions generate a call to the
plus method in the user-define class, if it exists:
q + p
p + q
Whether this method can add objects of class double and the user-defined class depends on how you
implement the method.
When p and q are objects of different classes, MATLAB applies the rules of precedence to determine
which method to use.
For more information on how MATLAB determines which method to call, see “Method Invocation” on
page 9-11.
Operator Precedence
Overloaded operators retain the original MATLAB precedence for the operator. For information on
operator precedence, see “Operator Precedence”.
17-19
17 Specialize Object Behavior
The Adder class also implements the less than operator (<) by defining a lt method. The lt method
returns a logical value after comparing the values in each object NumericData property.
classdef Adder
properties
NumericData
end
methods
function obj = Adder(val)
obj.NumericData = val;
end
function r = plus(obj1,obj2)
a = double(obj1);
b = double(obj2);
r = Adder(a + b);
end
function d = double(obj)
d = obj.NumericData;
end
function tf = lt(obj1,obj2)
if obj1.NumericData < obj2.NumericData
tf = true;
else
tf = false;
end
end
end
end
Using a double converter enables you to add numeric values to Adder objects and to perform
addition on objects of the class.
a = Adder(1:10)
a =
NumericData: [1 2 3 4 5 6 7 8 9 10]
ans =
NumericData: [2 4 6 8 10 12 14 16 18 20]
17-20
Operator Overloading
b = uint8(255) + a
b =
NumericData: [256 257 258 259 260 261 262 263 264 265]
a < b
ans =
Ensure that your class provides any error checking required to implement your class design.
17-21
17 Specialize Object Behavior
a:b colon(a,b)
a' ctranspose(a) Complex conjugate transpose
a.' transpose(a) Matrix transpose
[a b] horzcat(a,b,...) Horizontal concatenation
[a; b] vertcat(a,b,...) Vertical concatenation
a(s1,s2,...sn) subsref(a,s) Subscripted reference
a(s1,...,sn) = b subsasgn(a,s,b) Subscripted assignment
b(a) subsindex(a) Subscript index
See Also
Related Examples
• “Define Arithmetic Operators” on page 19-12
• “Methods That Modify Default Behavior” on page 17-2
17-22
Customize Parentheses Indexing for Mapping Class
The full code for the class and its helper function, validateKeys, is available at the end of the
example.
keys = string.empty(varargin{:});
if ~all(size(keys) == [0, 1])
error("MyMap:MustBeEmptyColumnVector",...
"The only supported empty size is 0x1.");
end
obj = MyMap(keys,cell.empty(varargin{:}));
end
end
17-23
17 Specialize Object Behavior
narginchk(2,2);
if ~all(size(keys_in) == size(values_in))
error("MyMap:InputSizesDoNotMatch",...
"The sizes of the input keys and values must match.");
end
obj.Keys = keys_in;
obj.Values = values_in;
end
end
function keys = getKeys(obj) Two public methods provide read access to the
keys = obj.Keys; keys and values.
end
17-24
Customize Parentheses Indexing for Mapping Class
if ~keyExists
error("MyMap:MultiLevelAssignKeyDoesNotExist", ...
"Assignment failed because key %s does not exist",...
indicesCell{1});
end
[obj.Values{idx}.(indexOp(2:end))] = varargin{:};
end
function obj = parenDelete(obj,indexOp) Implementation of the abstract method
[keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices);
parenDelete, which handles deletion
if keyExists indexing expressions of the form
obj.Keys(idx) = []; instanceName("keyName") = []. If the
obj.Values(idx) = [];
key referenced exists, then the method deletes
else
the key and its associated value. If the key
error("MyMap:DeleteNonExistentKey",...
does%snot
"Unable to perform deletion. The key exist,
does notthen the method issues an
exist.",...
indexOp(1).Indices{1}); error.
end
end
17-25
17 Specialize Object Behavior
keys = string.empty(varargin{:});
if ~all(size(keys) == [0, 1])
error("MyMap:MustBeEmptyColumnVector",...
"The only supported empty size is 0x1.");
end
obj = MyMap(keys,cell.empty(varargin{:}));
end
end
narginchk(2,2);
if ~all(size(keys_in) == size(values_in))
error("MyMap:InputSizesDoNotMatch",...
"The sizes of the input keys and values must match.");
end
obj.Keys = keys_in;
obj.Values = values_in;
end
17-26
Customize Parentheses Indexing for Mapping Class
end
function C = cat(dim,varargin)
error("MyMap:ConcatenationNotSupported",...
"Concatenation is not supported.");
end
requestedKey = keyCellArray{1};
idx = find(contains(obj.Keys,requestedKey));
keyExists = ~isempty(idx);
end
end
if ~keyExists
error("MyMap:KeyDoesNotExist",...
"The requested key does not exist.");
end
if numel(indexOp) == 1
nargoutchk(0,1);
varargout{1} = obj.Values{idx};
else
[varargout{1:nargout}] = obj.Values{idx}.(indexOp(2:end));
end
end
if numel(indexOp) == 1
value = varargin{1};
if keyExists
obj.Values{idx} = value;
else
obj.Keys(end+1) = indicesCell{1};
obj.Values{end+1} = value;
end
return;
end
if ~keyExists
error("MyMap:MultiLevelAssignKeyDoesNotExist", ...
"Assignment failed because key %s does not exist",...
indicesCell{1});
end
[obj.Values{idx}.(indexOp(2:end))] = varargin{:};
end
17-27
17 Specialize Object Behavior
[keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices);
if keyExists
obj.Keys(idx) = [];
obj.Values(idx) = [];
else
error("MyMap:DeleteNonExistentKey",...
"Unable to perform deletion. The key %s does not exist.",...
indexOp(1).Indices{1});
end
end
function n = parenListLength(obj,indexOp,indexingContext)
[keyExists,idx] = convertKeyToIndex(obj,indexOp(1).Indices);
if ~keyExists
if indexingContext == matlab.indexing.IndexingContext.Assignment
error("MyMap:MultiLevelAssignKeyDoesNotExist", ...
"Unable to perform assignment. Key %s does not exist",...
indexOp(1).Indices{1});
end
error("MyMap:KeyDoesNotExist",...
"The requested key does not exist.");
end
n = listLength(obj.Values{idx},indexOp(2:end),indexingContext);
end
end
end
function validateKeys(requestedKeysCell)
if numel(requestedKeysCell) == 0
error("MyMap:MustProvideAtLeastOneKey",...
"At least one key must be provided.");
end
if numel(requestedKeysCell) > 1
error("MyMap:MustUseOnlyOneSubscript",...
"Indexing with more than one subscript not supported.");
end
requestedKeys = requestedKeysCell{1};
if ~isstring(requestedKeys)
error("MyMap:InvalidKey","Keys must be strings.");
end
if numel(requestedKeys) == 0
error("MyMap:MustProvideAtLeastOneKey",...
"At least one key must be provided.");
end
if numel(requestedKeys) > 1
error("MyMap:UnableToUseMoreThanOneKey",...
"Unable to use more than one key.");
end
end
Save the code for MyMap and validateKeys in your MATLAB path. Create an instance of MyMap
with an initial list of three keys and values.
map = MyMap(["apple","cherry","orange"],{1,3,15});
Use the map("keyName") syntax to return the value corresponding to a specific key.
map("cherry")
17-28
Customize Parentheses Indexing for Mapping Class
ans =
map("banana") = 2;
map("banana")
ans =
Use the map("keyName") = [] to delete a key and its associated value from the array. Confirm the
key is no longer in the array.
map("orange") = [];
map("orange")
See Also
matlab.mixin.indexing.RedefinesParen | matlab.indexing.IndexingOperation
17-29
17 Specialize Object Behavior
obj(1).prop{7}
You can choose to customize only the parentheses indexing by inheriting from RedefinesParen and
then forward the remaining indexing operations (dot and brace) to the default behaviors. This class
fragment inherits from RedefinesParen:
The forwarding syntax is the dynamic dot indexing syntax with the IndexingOperation instance:
temp.(indexOp(2:end))
This expression handles all of the indexing operations after the first parentheses. In other words, the
indexing expression described by indexOp(2:end) is forwarded to temp. In this example,
indexOp(2) and indexOp(3) are the dot and brace indexing operations, respectively. temp.
(indexOp(2:end)) translates to:
temp.prop{7}
For customized dot indexing, the forwarding syntax maintains the access permissions from the
original context of the main indexing expression. For example, after the parentheses indexing
obj(1).prop{7} is handled, MATLAB handles the dot indexing, temp.prop{7}, using the same
context that the initial indexing expression started with. When you call obj(1).prop{7} inside the
class, the private property prop is accessible. When you call obj(1).prop{7} outside of the class,
prop is not accessible.
17-30
Forward Indexing Operations
See Also
matlab.mixin.indexing.RedefinesParen | matlab.mixin.indexing.RedefinesDot |
matlab.mixin.indexing.RedefinesBrace | matlab.indexing.IndexingOperation
Related Examples
• “Customize Parentheses Indexing for Mapping Class” on page 17-23
17-31
18
a.field1 = 7
a =
field1: 7
MATLAB provides user-defined classes with similar display functionality. User-defined classes can
customize how MATLAB displays objects of the class using the API provided by the
matlab.mixin.CustomDisplay class. To use this API, derive your class from
matlab.mixin.CustomDisplay.
• A header showing the class name, and the dimensions for nonscalar arrays.
• A list of all nonhidden public properties, shown in the order of definition in the class.
The actual display depends on whether the object is scalar or nonscalar. Also, there are special
displays for a scalar handle to a deleted object and empty object arrays. Objects in all of these states
are displayed differently if the objects have no properties.
The details function creates the default detailed display. The detailed display adds these items to
the simple display:
18-2
Custom Display Interface
See “Class with Default Object Display” on page 18-11 for an example of how MATLAB displays
objects.
MATLAB displays object properties that have public get access and are not hidden (see “Property
Attributes” on page 8-8). Inherited abstract properties are excluded from display. When the object
being displayed is scalar, any dynamic properties attached to the object are also included.
CustomDisplay Class
The matlab.mixin.CustomDisplay class provides an interface that you can use to customize
object display for your class. To use this interface, derive your class from CustomDisplay:
The CustomDisplay class is HandleCompatible, so you can use it in combination with both value
and handle superclasses.
Note You cannot use matlab.mixin.CustomDisplay to derive a custom display for enumeration
classes.
The CustomDisplay interface does not allow you to override disp, display, and details.
Instead, override any combination of the customization methods defined for this purpose.
• Part builder methods build the strings used for the standard display. Override any of these
methods to change the respective parts of the display.
• State handler methods are called for objects in specific states, like scalar, nonscalar, and so on.
Override any of these methods to handle objects in a specific state.
All of these methods have protected access and must be defined as protected in your subclass of
CustomDisplay (that is, Access = protected).
There are three parts that makeup the standard object display — header, property list, and footer
For example, here is the standard object display for a containers.Map object:
18-3
18 Customizing Object Display
The default object display does not include a footer. The detailed display provides more information:
You can customize how MATLAB displays objects as a result of expressions that display objects in the
command window such as unterminated statements that return objects or calls to disp and
display. The results displayed when calling details on an object or object array are not changed
by the CustomDisplay API.
Each part of the object display has an associated method that assembles the respective part of the
display.
18-4
Custom Display Interface
There are four object states that affect how MATLAB displays objects:
Each object state has an associated method that MATLAB calls whenever displaying objects that are
in that particular state.
Utility Methods
The CustomDisplay class provides utility methods that return strings that are used in various parts
of the different display options. These static methods return text that simplifies the creation of
customized object displays.
If the computer display does not support hypertext linking, the strings are returned without the links.
18-5
18 Customizing Object Display
See Also
Related Examples
• “How CustomDisplay Works” on page 18-7
18-6
How CustomDisplay Works
For example, suppose obj is a valid scalar object of a class derived from CustomDisplay. If you type
obj at the command line without terminating the statement with a semicolon:
>> obj
MATLAB follows a similar sequence for nonscalar object arrays and empty object arrays.
Only an instance of a handle class can be in a state of scalar handle to a deleted object.
18-7
18 Customizing Object Display
See Also
Related Examples
• “Class with Default Object Display” on page 18-11
18-8
Role of size Function in Custom Displays
An unusual or improper implementation of size can result in undesirable display behavior. For
example, suppose a class overloads size reports an object as scalar when it is not. In this class, a
property list consisting of a cell array of strings results in the property values of the first object of the
array being displayed. This behavior can give the impression that all objects in the array have the
same property values.
However, reporting an object as scalar when in fact the object is empty results in the object
displaying as an empty object array. The default methods of the CustomDisplay interface always
determine if the input is an empty array before attempting to access property values.
As you override CustomDisplay methods to implement your custom object display, consider how an
overloading size method can affect the result.
See Also
Related Examples
• “Methods That Modify Default Behavior” on page 17-2
18-9
18 Customizing Object Display
The versions of disp and display that are inherited from matlab.mixin.CustomDisplay are
sealed. However, these methods call all of the part builder (“Part Builder Methods” on page 18-4) and
state handler methods (“State Handler Methods” on page 18-5).
To use the CustomDisplay interface, the root class of the heterogeneous hierarchy can declare
these methods as Sealed and Access = protected.
If you do not need to override a particular method, then call the superclass method, as shown in the
following code.
For example, the following code shows modifications to the getPropertyGroups and
displayScalarObject methods, while using the superclass implementation of all others.
classdef RootClass < matlab.mixin.CustomDisplay & matlab.mixin.Heterogeneous
%...
methods (Sealed, Access = protected)
function header = getHeader(obj)
header = getHeader@matlab.mixin.CustomDisplay(obj);
end
function displayNonScalarObject(obj)
displayNonScalarObject@matlab.mixin.CustomDisplay(obj);
end
function displayScalarObject(obj)
% Override of this method
% ...
end
function displayEmptyObject(obj)
displayEmptyObject@matlab.mixin.CustomDisplay(obj);
end
function displayScalarHandleToDeletedObject(obj)
displayScalarHandleToDeletedObject@matlab.mixin.CustomDisplay(obj);
end
end
end
See Also
Related Examples
• “Designing Heterogeneous Class Hierarchies” on page 10-18
18-10
Class with Default Object Display
In this section...
“The EmployeeInfo Class” on page 18-11
“Default Display — Scalar” on page 18-11
“Default Display — Nonscalar” on page 18-12
“Default Display — Empty Object Array” on page 18-12
“Default Display — Handle to Deleted Object” on page 18-13
“Default Display — Detailed Display” on page 18-13
EmployeeInfo is also a handle class. Therefore instances of this class can be in the state referred to
as a handle to a deleted object. This state does not occur with value classes (classes not derived from
handle).
classdef EmployeeInfo < handle & matlab.mixin.CustomDisplay
properties
Name
JobTitle
Department
Salary
Password
end
methods
function obj = EmployeeInfo
obj.Name = input('Name: ');
obj.JobTitle = input('Job Title: ');
obj.Department = input('Department: ');
obj.Salary = input('Salary: ');
obj.Password = input('Password: ');
end
end
end
18-11
18 Customizing Object Display
>>Emp123 = EmployeeInfo;
Name: 'Bill Tork'
Job Title: 'Software Engineer'
Department: 'Product Development'
Salary: 1000
Password: 'bill123'
>>Emp123
Emp123 =
>>[Emp123,Emp124]
ans
Name
JobTitle
Department
Salary
Password
Empt =
Name
18-12
Class with Default Object Display
JobTitle
Department
Salary
Password
Use isempty to test for empty object arrays. An empty object array is not scalar because its
dimensions can never be 1–by-1.
emt =
Name
JobTitle
Department
Salary
Password
>> isscalar(emt)
ans =
>> delete(Emp123)
>> Emp123
Emp123 =
handle to deleted EmployeeInfo
Note isvalid is a handle class method. Calling isvalid on a value class object causes an error.
details(Emp123)
EmployeeInfo handle with properties:
18-13
18 Customizing Object Display
See Also
Related Examples
• “Custom Display Interface” on page 18-2
18-14
Choose a Technique for Display Customization
If you are making small changes to the default layout, then override the relevant part builder
methods (“Part Builder Methods” on page 18-4). For example, suppose you want to:
• Change the order or value of properties, display a subset of properties, or create property groups
• Modify the header text
• Add a footer
If you are defining a nonstandard display for a particular object state (scalar, for example), then the
best approach is to override the appropriate state handler method (“State Handler Methods” on page
18-5).
In some cases, a combination of method overrides might be the best approach. For example, your
implementation of displayScalarObject might
• Use some of the utility methods (“Utility Methods” on page 18-5) to build your own display strings
using parts from the default display
• Call a part builder method to get the default text for that particular part of the display
• Implement a completely different display for scalar objects.
Once you override any CustomDisplay method, MATLAB calls your override in all cases where the
superclass method would have been called. For example, if you override the getHeader method,
your override must handle all cases where a state handler method calls getHeader. (See “Methods
Called for a Given Object State” on page 18-7)
Use a nonstandard layout for scalar object display that is fully defined in the displayScalarObject
method:
classdef MyClass < matlab.mixin.CustomDisplay
...
methods (Access = protected)
function displayScalarObject(obj)
% Implement the custom display for scalar obj
18-15
18 Customizing Object Display
end
end
end
Use standard display layout, but create a custom property list for scalar and nonscalar display:
classdef MyClass < matlab.mixin.CustomDisplay
...
methods(Access = protected)
function groups = getPropertyGroups(obj)
% Return PropertyGroup instances
end
end
end
Use standard display layout, but create a custom property list for scalar only. Call the superclass
getPropertyGroups for the nonscalar case.
classdef MyClass < matlab.mixin.CustomDisplay
properties
Prop1
Prop2
Prop3
end
methods(Access = protected)
function groups = getPropertyGroups(obj)
if isscalar(obj)
% Scalar case: change order
propList = {'Prop2','Prop1','Prop3'};
groups = matlab.mixin.util.PropertyGroup(propList)
else
% Nonscalar case: call superclass method
groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
end
end
end
end
Change the values displayed for some properties in the scalar case by creating property/value pairs in
a struct. This getPropertyGroups method displays only Prop1 and Prop2, and displays the value
of Prop2 as Prop1 divided by Prop3.
classdef MyClass < matlab.mixin.CustomDisplay
properties
Prop1
Prop2
Prop3
end
methods(Access = protected)
function groups = getPropertyGroups(obj)
if isscalar(obj)
% Specify the values to be displayed for properties
propList = struct('Prop1',obj.Prop1,...
'Prop2',obj.Prop1/obj.Prop3);
groups = matlab.mixin.util.PropertyGroup(propList)
else
% Nonscalar case: call superclass method
groups = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
end
end
18-16
Choose a Technique for Display Customization
end
end
18-17
18 Customizing Object Display
Objective
Change the order and number of properties displayed for an object of your class.
In the default scalar object display, MATLAB displays all the public properties along with their values.
However, you want to display only Department, JobTitle, and Name, in that order. You can do this
by deriving from CustomDisplay and overriding the getPropertyGroups method.
Your override
• Defines method Access as protected to match the definition in the CustomDisplay superclass
• Creates a cell array of property names in the desired order
• Returns a PropertyGroup object constructed from the property list cell array
methods (Access = protected)
function propgrp = getPropertyGroups(~)
proplist = {'Department','JobTitle','Name'};
propgrp = matlab.mixin.util.PropertyGroup(proplist);
end
end
When you create a PropertyGroup object using a cell array of property names, MATLAB
automatically
The getPropertyGroups method is not called to create the display for a scalar handle to a deleted
object.
18-18
Customize Property Display
override of the getPropertyGroups method uses the default property display for nonscalar objects
by calling the superclass getPropertyGroups method. For scalar objects, the override:
• Changes the value displayed for the Password property to a '*' character for each character in
the password.
• Displays the text 'Not Available' for the Salary property.
methods (Access = protected)
function propgrp = getPropertyGroups(obj)
if ~isscalar(obj)
propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
else
pd(1:length(obj.Password)) = '*';
propList = struct('Department',obj.Department,...
'JobTitle',obj.JobTitle,...
'Name',obj.Name,...
'Salary','Not available',...
'Password',pd);
propgrp = matlab.mixin.util.PropertyGroup(propList);
end
end
end
18-19
18 Customizing Object Display
See Also
Related Examples
• “Choose a Technique for Display Customization” on page 18-15
18-20
Customize Header, Property List, and Footer
Objective
Customize each of the three parts of the display — header, property groups, and footer.
Note This example uses the EmployeeInfo class described in the “Class with Default Object
Display” on page 18-11 section.
For properties:
• Nonscalar object arrays display a subset of property names in a different order than the default.
• Scalar objects create two property groups that have titles (Public Info and Personal Info).
• Add a footer to the display, only when the object is a valid scalar that displays property values.
Emp123 =
Public Info
Name: 'Bill Tork'
JobTitle: 'Software Engineer'
Personal Info
Salary: 1000
Password: 'bill123'
Company Private
18-21
18 Customizing Object Display
[Emp123,Emp124]
ans =
Department
Name
JobTitle
>> EmployeeInfo.empty(0,5)
ans =
Department
Name
JobTitle
>> delete(Emp123)
>> Emp123
Emp123 =
Implementation
• getHeader
• getPropertyGroups
• getFooter
Each method must produce the desired results with each of the following inputs:
• Scalar object
• Nonscalar object array
• Empty object array
• Nonscalar (including empty object) arrays call the superclass getHeader, which returns the
default header.
• Scalar handles to deleted objects do not result in a call to getHeader.
18-22
Customize Header, Property List, and Footer
• Scalar inputs build a custom header using the getClassNameForHeader static method to return
linked class name text, and the value of the Department property.
Here is the EmployeeInfo override of the getHeader method. The required protected access is
inherited from the superclass.
methods (Access = protected)
function header = getHeader(obj)
if ~isscalar(obj)
header = getHeader@matlab.mixin.CustomDisplay(obj);
else
className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
newHeader = [className,' Dept: ',obj.Department];
header = sprintf('%s\n',newHeader);
end
end
end
getPropertyGroups Override
MATLAB calls getPropertyGroups to get the PropertyGroup objects, which control how
properties are displayed. This method override defines two different property lists depending on the
object’s state:
• For nonscalar inputs, including empty arrays and arrays containing handles to deleted objects,
create a property list as a cell array to reorder properties.
By default, MATLAB does not display property values for nonscalar inputs.
• For scalar inputs, create two property groups with titles. The scalar code branch lists properties in
a different order than the nonscalar case and includes Salary and Password properties.
MATLAB automatically assigns property values.
• Scalar handles to deleted objects do not result in a call to getPropertyGroups.
Here is the EmployeeInfo override of the getPropertyGroups method. The protected access is
inherited from the superclass.
methods (Access = protected)
function propgrp = getPropertyGroups(obj)
if ~isscalar(obj)
propList = {'Department','Name','JobTitle'};
propgrp = matlab.mixin.util.PropertyGroup(propList);
else
gTitle1 = 'Public Info';
gTitle2 = 'Personal Info';
propList1 = {'Name','JobTitle'};
propList2 = {'Salary','Password'};
propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1);
propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2);
end
end
end
getFooter Override
MATLAB calls getFooter to get the footer text. The EmployeeInfo getFooter method defines a
footer for the display, which is included only when the input is a valid scalar object. In all other cases,
getFooter returns an empty char vector.
18-23
18 Customizing Object Display
18-24
Customize Header, Property List, and Footer
See Also
Related Examples
• “Choose a Technique for Display Customization” on page 18-15
18-25
18 Customizing Object Display
Objective
Customize the display of scalar objects.
Note This example uses the EmployeeInfo class described in the “Class with Default Object
Display” on page 18-11 section.
• Modify the header to include the department name obtained from the Department property
• Group properties into two categories titled Public Info and Personal Info.
• Modify which properties are displayed
• Modify the values displayed for Personal Info category
• Use the default displayed for nonscalar objects, including empty arrays, and scalar deleted
handles
For example, here is the customized display of an object of the EmployeeInfo class.
Emp123 =
Public Info
Name: 'Bill Tork'
JobTitle: 'Software Engineer'
Personal Info
Salary: 'Level: 10'
Password: '*******'
Implementation
18-26
Customize Display of Scalar Objects
This implementation:
• Builds a custom header using the getClassNameForHeader static method to return linked class
name text and the value of the Department property to get the department name.
• Uses sprintf to add a new line to the header text
• Displays the header with the built-in disp function.
• Calls the getPropertyGroups override to define the property groups (see following section).
• Displays the property groups using the displayPropertyGroups static method.
Here is the EmployeeInfo override of the displayScalarObject method. The required protected
access is inherited from the superclass.
methods (Access = protected)
function displayScalarObject(obj)
className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
scalarHeader = [className,' Dept: ',obj.Department];
header = sprintf('%s\n',scalarHeader);
disp(header)
propgroup = getPropertyGroups(obj);
matlab.mixin.CustomDisplay.displayPropertyGroups(obj,propgroup)
end
end
getPropertyGroups Override
MATLAB calls getPropertyGroups when displaying scalar or nonscalar objects. However, MATLAB
does not call this method when displaying a scalar handle to a deleted object.
The EmployeeInfo class overrides this method to implement the property groups for scalar object
display.
This implementation calls the superclass getPropertyGroups method if the input is not scalar. If
the input is scalar, this method:
Here is the EmployeeInfo override of the getPropertyGroups method. The required protected
access is inherited from the superclass.
methods (Access = protected)
function propgrp = getPropertyGroups(obj)
18-27
18 Customizing Object Display
if ~isscalar(obj)
propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
else
gTitle1 = 'Public Info';
gTitle2 = 'Personal Info';
propList1 = {'Name','JobTitle'};
pd(1:length(obj.Password)) = '*';
level = round(obj.Salary/100);
propList2 = struct('Salary',...
['Level: ',num2str(level)],...
'Password',pd);
propgrp(1) = matlab.mixin.util.PropertyGroup(propList1,gTitle1);
propgrp(2) = matlab.mixin.util.PropertyGroup(propList2,gTitle2);
end
end
end
18-28
Customize Display of Scalar Objects
See Also
Related Examples
• “Choose a Technique for Display Customization” on page 18-15
18-29
18 Customizing Object Display
Objective
Customize the display of nonscalar objects, including empty object arrays.
Note This example uses the EmployeeInfo class described in the “Class with Default Object
Display” on page 18-11 section.
2. Employee:
Name: 'Alice Blackwell'
Department: 'QE'
3. Employee:
Name: 'Nancy Green'
Department: 'Documentation'
1. Employee:
Name: 'Bill Tork'
Department: 'Product Development'
18-30
Customize Display of Object Arrays
3. Employee:
Name: 'Nancy Green'
Department: 'Documentation'
To achieve the desired result, the EmployeeInfo class overrides the following methods of the
matlab.mixin.CustomDisplay class:
• Builds header text using convertDimensionsToString to obtain the array size and
getClassNameForHeader to get the class name with a link to the help for that class.
• Displays the modified header text.
• Loops through the elements in the array, building two different subheaders depending on the
individual object state. In the loop, this method:
• Detects handles to deleted objects (using the isvalid handle class method). Uses
getDeletedHandleText and getClassNameForHeader to build text for array elements that
are handles to deleted objects.
• Builds a custom subheader for valid object elements in the array
• Creates a PropertyGroup object containing the Name and Department properties for valid
objects
• Uses the displayPropertyGroups static method to generate the property display for valid
objects.
18-31
18 Customizing Object Display
• Gets the array dimensions in character format using the convertDimensionsToString static
method.
• Gets text with the class name linked to the helpPopup function using the
getClassNameForHeader static method.
• Builds and displays the custom text for empty arrays.
methods (Access = protected)
function displayEmptyObject(obj)
dimstr = matlab.mixin.CustomDisplay.convertDimensionsToString(obj);
className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
emptyHeader = [dimstr,' ',className,' with no employee information'];
header = sprintf('%s\n',emptyHeader);
disp(header)
end
end
Empt = EmployeeInfo.empty(0,5)
Empt =
18-32
Customize Display of Object Arrays
else
numStr = [num2str(ix),'. Employee'];
disp(numStr)
propList = struct('Name',o.Name,...
'Department',o.Department);
propgrp = matlab.mixin.util.PropertyGroup(propList);
matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp);
end
end
end
function displayEmptyObject(obj)
dimstr = matlab.mixin.CustomDisplay.convertDimensionsToString(obj);
className = matlab.mixin.CustomDisplay.getClassNameForHeader(obj);
emptyHeader = [dimstr,' ',className,' with no employee information'];
header = sprintf('%s\n',emptyHeader);
disp(header)
end
end
end
See Also
Related Examples
• “Choose a Technique for Display Customization” on page 18-15
18-33
18 Customizing Object Display
Display Methods
Subclassing matlab.mixin.CustomDisplay is the best approach to customizing object display.
However, if you do not derive your class from matlab.mixin.CustomDisplay, overload the disp
function to change how MATLAB displays objects of your class.
MATLAB calls the display function whenever an object is referred to in a statement that is not
terminated by a semicolon. For example, the following statement creates the variable a. MATLAB
calls display, which displays the value of a in the command line.
a = 5
a =
5
Overloaded disp
The built-in display function prints the name of the variable that is being displayed, if an
assignment is made, or otherwise uses ans as the variable name. Then display calls disp to handle
the actual display of the values.
If the variable that is being displayed is an object of a class that overloads disp, then MATLAB
always calls the overloaded method. MATLAB calls display with two arguments and passes the
variable name as the second argument.
• MATLAB executes a statement that returns a value and is not terminated with a semicolon.
• There is no left-side variable, then MATLAB prints ans = followed by the value.
• Code explicitly invokes the display function.
• If the input argument is an existing variable, display prints the variable name and equal sign,
followed by the value.
• If the input is the result of an expression, display does not print ans =.
MATLAB invokes the built-in disp function when the following occurs:
18-34
Overloading the disp Function
For empty built-in types (numeric types, char, struct, and cell) the display function displays:
See Also
Related Examples
• “Custom Display Interface” on page 18-2
• “Overload disp for DocPolynom” on page 19-11
18-35
18 Customizing Object Display
• Single-line layout — The display of the object array is confined to a single line. This display
layout occurs when the object array is contained within a structure, cell array, or property of a
MATLAB object. Override the CustomCompactDisplayProvider method
compactRepresentationForSingleLine to customize this layout.
• Columnar layout — The object array is displayed in multiple rows as part of column-oriented or
tabular data. This display layout occurs when the object array is contained within a table variable.
Override the CustomCompactDisplayProvider method compactRepresentationForColumn
to customize this layout.
Three additional CustomCompactDisplayProvider utility methods enable you to control how much
of the data is used to construct a display of the object array:
Each of these three utility methods also provides an Annotation name-value argument, which
enables you to include a row vector of strings to serve as descriptors in your compact displays. For
example, in the case of a class representing polynomials, adding an annotation can be useful when
only the dimensions and class of the data fit in the display:
18-36
Custom Compact Display Interface
classdef Polynomial
properties
polynomialString string {mustBeScalarOrEmpty};
polynomialOrder double {mustBeScalarOrEmpty};
end
methods
function obj = Polynomial(polyString,polyOrder)
obj.polynomialString = polyString;
obj.polynomialOrder = polyOrder;
end
end
end
The full code of this class with customized compact display functionality is available at the end of the
example.
Create an instance of Polynomial. Display the instance in a cell array to check the single-line layout.
poly = "(x^10)/100 + 300*x^9 + 200*x^8 + 4000*x^7 + 600*x^6 + 700*x^5 + 45*x^4 + 90*x^3 + 4*x^2 +
a = Polynomial(poly,10);
{a}
ans =
{1×1 Polynomial}
The single-line layout does not provide users with much information. Change Polynomial to inherit
from matlab.mixin.CustomCompactDisplayProvider and override the
compactRepresentationForSingleLine method. Call the utility method
widthConstrainedDataRepresentation with AllowTruncatedDisplayForScalar set to
true. MATLAB now truncates the polynomial string based on the width available.
function displayRep = compactRepresentationForSingleLine(obj,displayConfiguration,width)
displayRep = widthConstrainedDataRepresentation(obj,displayConfiguration,width,...
StringArray=obj.polynomialString,AllowTruncatedDisplayForScalar=true);
end
Now, the display of the instance in a cell array contains as much of the string as possible.
{a}
ans =
For a table display, the default shows only the class and dimensions, so override
compactRepresentationForColumn. The method returns an instance of
matlab.display.DimensionsAndClassNameRepresentation with an annotation, which in this
case identifies the order of the polynomial. To cover the case when a table has a row vector of
Polynomial instances in a single entry, the method reshapes the polynomialOrder property to
match the shape of the instance and then finds the maximum order in that row for display.
function displayRep = compactRepresentationForColumn(obj,displayConfig,width)
import matlab.display.DimensionsAndClassNameRepresentation;
18-37
18 Customizing Object Display
polOrder = reshape([obj.polynomialOrder],size(obj));
maxPolOrderPerRow = max(polOrder,[],2);
annotationStr = "Polynomial of order " + string(maxPolOrderPerRow);
displayRep = DimensionsAndClassNameRepresentation(obj,...
displayConfig,Annotation=annotationStr);
end
With this code in place, you can create a table of estimation results, for example, that identifies the
order of the polynomial. Create a second Polynomial, b. Then create two tables, one with a and b in
a column, and one with a and b in the same row.
b = Polynomial("x^3 + x^2 + x",3);
Result = [1;2];
Estimate = [a;b];
T = table(Result,Estimate)
T =
2×2 table
Result Estimate
______ _________________________________________
With a and b in the same row, only the maximum order is displayed.
Result = 1;
Estimate = [a b];
T1 = table(Result,Estimate)
T1 =
1×2 table
Result Estimate
_______ _________________________________________
18-38
Custom Compact Display Interface
See Also
Related Examples
• matlab.mixin.CustomCompactDisplayProvider
18-39
19
In this section...
“Class Requirements” on page 19-2
“DocPolynom Class Members” on page 19-2
“DocPolynom Class Synopsis” on page 19-4
“The DocPolynom Constructor” on page 19-9
“Convert DocPolynom Objects to Other Classes” on page 19-10
“Overload disp for DocPolynom” on page 19-11
“Display Evaluated Expression” on page 19-11
“Define Arithmetic Operators” on page 19-12
“Redefine Parentheses Indexing” on page 19-13
You can use classes to define new data types. This example implements a class that represents
polynomials. The class stores the coefficients of the polynomial terms in a vector and overrides the
default MATLAB display to show the polynomials as powers of x. Using customized indexing, the class
also enables you to evaluate the polynomials at one or more values of x using parentheses indexing
syntax.
Class Requirements
The design requirements for the DocPolynom class are:
• Value class behavior —Behave like MATLAB numeric variables when copied and passed to
functions.
• Scalar object behavior — Polynomial objects cannot be concatenated, and polynomial array size
must always be (1,1).
• Customized indexing behavior — Evaluate a polynomial using parentheses indexing syntax. p(x)
evaluates the polynomial represented by object p at each value in x.
• Specialized display — Use the coefficients stored in the polynomial object to display the
polynomial as an algebraic expression.
• Override addition, subtraction, and multiplication — Adding, subtracting, or multiplying
polynomial objects returns the result of the corresponding algebraic operation on the two
polynomials.
• Double converter — Convert a polynomial object to a double array so it can be used with existing
MATLAB functions that accept numeric inputs.
19-2
Representing Polynomials with Classes
These examples show the basic use of the DocPolynom class. Create DocPolynom objects to
represent f(x) = x3 − 2x − 5 and f(x) = 2x4 + 3x2 + 2x − 7.
p1 = DocPolynom([1 0 -2 -5])
p1 =
x^3 - 2*x - 5
p2 = DocPolynom([2 0 3 2 -7])
p2 =
2*x^4 + 3*x^2 + 2*x - 7
Find the roots of the polynomial p1. Use the double method of the object and pass the result to the
roots function.
roots(double(p1))
ans =
2.0946 + 0.0000i
-1.0473 + 1.1359i
-1.0473 - 1.1359i
Add the two polynomials p1 and p2. MATLAB calls the plus method defined for the DocPolynom
class when you add two DocPolynom objects.
19-3
19 Defining Custom Data Types
p1 + p2
ans =
19-4
Representing Polynomials with Classes
19-5
19 Defining Custom Data Types
function r = mtimes(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
r = DocPolynom(conv(obj1.coef,obj2.coef));
end
end
methods (Access = protected) Customize parentheses reference for
function f = parenReference(obj,indexOp) DocPolynom objects. This
n = cell2mat(indexOp(1).Indices); implementation of parenReference
if numel(indexOp) == 1 enables users to evaluate a
f = polyval(obj.coef,n);
polynomial using parentheses
else
indexing syntax. p(x) evaluates the
f = polyval(obj.coef,n).(indexOp(2:end));
end polynomial represented by object p
end for each value in x.
end
end For more information, see “Redefine
Parentheses Indexing” on page 19-
13.
properties
coef
end
19-6
Representing Polynomials with Classes
methods
function obj = DocPolynom(c)
if nargin > 0
if isa(c,'DocPolynom')
obj.coef = c.coef;
else
obj.coef = c(:).';
end
end
end
function c = double(obj)
c = obj.coef;
end
19-7
19 Defining Custom Data Types
ind = ind + 1;
end
end
end
if d >= 2
s(ind) = {['x^' int2str(d)]};
ind = ind + 1;
elseif d == 1
s(ind) = {'x'};
ind = ind + 1;
end
end
d = d - 1;
end
end
str = [s{:}];
end
function disp(obj)
c = char(obj);
if iscell(c)
disp([' ' c{:}])
else
disp(c)
end
end
function dispPoly(obj,x)
p = char(obj);
y = zeros(length(x));
disp(['f(x) = ',p])
for k = 1:length(x)
y(k) = polyval(obj.coef,x(k));
disp([' f(',num2str(x(k)),') = ',num2str(y(k))])
end
end
function r = plus(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] + [zm,obj2.coef]);
end
function r = minus(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] - [zm,obj2.coef]);
end
function r = mtimes(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
19-8
Representing Polynomials with Classes
r = DocPolynom(conv(obj1.coef,obj2.coef));
end
end
• An existing DocPolynom object — Calling the constructor with an existing DocPolynom object as
an input argument returns a new DocPolynom object with the same coefficients as the input
argument. The isa function checks for this input.
• Coefficient vector — When the input argument is not a DocPolynom object, the constructor
attempts to reshape the values into a row vector and assign them to the coef property.
The coef property set method restricts property values to doubles. See “Remove Leading Zeros”
on page 19-9 for a description of the property set method.
This example uses a vector as the input argument to the DocPolynom constructor:
p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x -5
This statement creates an instance of the DocPolynom class with the specified coefficients. The
display of the object shows the equivalent polynomial using MATLAB language syntax. The
DocPolynom class implements this display using the disp and char class methods.
The DocPolynom class represents polynomials as row vectors containing coefficients ordered by
descending powers. Zeros in the coefficient vector represent terms that are not in the polynomial.
19-9
19 Defining Custom Data Types
Leading zeros, therefore, can be ignored when forming the polynomial. In fact, some DocPolynom
class methods use the length of the coefficient vector to determine the degree of the polynomials, so
removing leading zeros from the coefficient vector ensures that the vector length represents the
correct polynomial degree.
The DocPolynom class stores the coefficient vector in a property that uses a set method to remove
leading zeros from the specified coefficients before setting the property value.
• double — Converts to the double numeric type so functions can perform mathematical operations
on the coefficients.
• char — Converts to characters used to format output for display in the Command Window.
The double converter method for the DocPolynom class returns the coefficient vector:
function c = double(obj)
c = obj.coef;
end
p = DocPolynom([1 0 -2 -5]);
c = double(p)
c =
1 0 -2 -5
The char method returns a char vector that represents the polynomial displayed as powers of x. The
char vector returned is a syntactically correct MATLAB expression.
The char method uses a cell array to collect the char vector components that make up the displayed
polynomial. The disp method uses the char method to format the DocPolynom object for display.
Users of DocPolynom objects are not likely to call the char or disp methods directly, but these
methods enable the DocPolynom class to behave like other data classes in MATLAB.
19-10
Representing Polynomials with Classes
The char method returns a cell array or the character '0' if the coefficients are all zero.
function disp(obj)
c = char(obj);
if iscell(c)
disp([' ' c{:}])
else
disp(c)
end
end
This statement creates a DocPolynom object. Because the statement is not terminated with a
semicolon, the resulting output is displayed on the command line using the overloaded disp method.
p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x - 5
function dispPoly(obj,x)
p = char(obj);
y = zeros(length(x));
disp(['f(x) = ',p,])
for k = 1:length(x)
y(k) = polyval(obj.coef,x(k));
disp([' f(',num2str(x(k)),') = ',num2str(y(k))])
end
end
p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x - 5
Evaluate the polynomial at three values of x, [3 5 9]. Instead of returning a vector of values, the
method uses function notation to present the results in an organized list.
dispPoly(p,[3 5 9])
19-11
19 Defining Custom Data Types
f(5) = 110
f(9) = 706
The overloaded plus, minus, and mtimes methods accept argument pairs that include at least one
DocPolynom object.
If either p or q is a DocPolynom object, this expression generates a call to the plus method overload
defined by DocPolynom unless the other object is of higher precedence.
p + q
This method overloads the plus (+) operator for the DocPolynom class.
function r = plus(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] + [zm,obj2.coef]);
end
• Ensure that both input arguments are DocPolynom objects so that expressions that involve a
DocPolynom and a double work correctly.
• Access the two coefficient vectors and, if necessary, pad one of them with zeros to make both the
same length. The actual addition is simply the vector sum of the two coefficient vectors.
• Call the DocPolynom constructor to create a properly typed object that is the result of adding the
polynomials.
The minus operator (-) uses the same approach as the plus (+) operator. The minus method
computes p - q. The dominant argument must be a DocPolynom object.
function r = minus(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
k = length(obj2.coef) - length(obj1.coef);
zp = zeros(1,k);
zm = zeros(1,-k);
r = DocPolynom([zp,obj1.coef] - [zm,obj2.coef]);
end
19-12
Representing Polynomials with Classes
The mtimes method computes the product p*q. The mtimes method implements matrix
multiplication because the multiplication of two polynomials is the convolution (conv) of their
coefficient vectors:
methods
function r = mtimes(obj1,obj2)
obj1 = DocPolynom(obj1);
obj2 = DocPolynom(obj2);
r = DocPolynom(conv(obj1.coef,obj2.coef));
end
end
p = DocPolynom([1 0 -2 -5]);
These two arithmetic operations call the DocPolynom plus and mtimes methods.
q = p + 1
r = p*q
q =
x^3 - 2*x - 4
r =
x^6 - 4*x^4 - 9*x^3 + 4*x^2 + 18*x + 20
p = DocPolynom([1 0 -2 -5])
p =
x^3 - 2*x - 5
The overloaded parenReference method evaluates the value of the polynomial at x = 3 and at x =
4 using this command.
p([3 4])
ans =
16 51
The parenReference method handles expressions of the form p(x), where p is a DocPolynom
object and x contains numeric inputs. Instead of a traditional MATLAB indexing operation, however,
19-13
19 Defining Custom Data Types
parenReference uses polyval to evaluate the polynomial using the coefficients stored in the coef
property.
For example, attempting to evaluate a polynomial object while also using dot indexing to try to
access its coef property at the same time errors.
p(5).coef(1)
For more information on forwarding operations with modular indexing, see “Forward Indexing
Operations” on page 17-30.
Using the modular indexing class in this way means that only parentheses reference operations are
customized. Dot access to properties and methods are unaffected and are handled by MATLAB as
expected.
19-14
20
A class design based on heterogeneous arrays provides a more convenient interface than, for
example, extracting elements from a cell array and operating on these elements individually. For
more information on the design of class hierarchies that support heterogeneous arrays, see
“Designing Heterogeneous Class Hierarchies” on page 10-18.
Note This example does not use valid terminology or techniques for managing financial assets. The
purpose of this example is only to illustrate techniques for defining heterogeneous class hierarchies.
This example implements a system of classes to represent financial assets, such as stocks, bonds, and
cash. Classes to represent categories of assets have certain common requirements. Each instance has
one of the following:
• Textual description
• Type (stock, bond, or cash)
• Means to determine the current value of the asset
Heterogeneous arrays of these objects need methods that can operate on the whole array. These
operations include:
These requirements are factored into the class that is the root of the hierarchy. The root class derives
from matlab.mixin.Heterogeneous. In the following diagram, the Assets class is the root of the
20-2
A Class Hierarchy for Heterogeneous Arrays
hierarchy. The Stocks, Bonds, and Cash classes provide the specialization required for each type of
asset.
20-3
20 Designing Related Classes
Assets Class
The Assets class:
Properties
Methods
• pie — A sealed method that creates a pie chart showing the relative mix of asset types.
• makeReport — A sealed method that creates a report listing the assets.
• getCurrentValue — An abstract method that each concrete subclass must implement to return
the current value of the asset.
• getDefaultScalarElement — matlab.mixin.Heterogeneous class method overridden in
the Assets class to specify a default object. The Assets class is abstract so it cannot be used as
the default object. For more information, see “Default Object” on page 20-9.
Concrete methods defined by superclasses in a heterogeneous hierarchy must specify the Sealed
attribute. Sealing these methods prevents subclasses from overriding methods implemented by the
superclass. When calling methods on a heterogeneous array, MATLAB calls the methods defined by
the class of the array (Assets in this example).
The pie and makeReport methods are examples of sealed methods that operate on heterogeneous
arrays composed of Stock, Bond, and Cash objects.
Abstract methods defined by the superclasses in a heterogeneous hierarchy must specify the
Abstract attribute. Defining an abstract method in a superclass ensures that concrete subclasses
have an implementation for that exact method name. Use these methods element-wise so that each
object calls its own method.
Each type of subclass object calculates its current value in a different way. If you add another
category of asset by adding another subclass to the hierarchy, this class must implement its own
20-4
A Class Hierarchy for Heterogeneous Arrays
For more information on the Sealed and Abstract method attributes, see “Method Attributes” on
page 9-4.
The Assets class and other classes in the hierarchy are contained in a package called financial.
classdef Assets < matlab.mixin.Heterogeneous
% file: +financial/@Assets/Assets.m
properties
Description char = 'Assets'
end
properties (Abstract, SetAccess = private)
Type
end
methods (Abstract)
% Not implemented by Assets class
value = getCurrentValue(obj)
end
methods (Static, Sealed, Access = protected)
function defaultObject = getDefaultScalarElement
defaultObject = financial.DefaultAsset;
end
end
methods (Sealed)
% Implemented in separate files
% +financial/@Assets/pie.m
% +financial/@Assets/makeReport.m
pie(assetArray)
makeReport(assetArray)
end
end
For code listings for pie and makeReport, see “Operating on an Assets Array” on page 20-11.
Stocks Class
The Stocks class represents a specific type of financial asset. It is a concrete class that implements
the abstract members defined by the Assets class, and defines class properties and methods specific
to this type of asset.
Properties
20-5
20 Designing Related Classes
Methods
• Stocks — The constructor assigns property values and supports a default constructor called with
no input arguments.
• getCurrentValue — This method is the Stocks class implementation of the abstract method
defined by the Assets class. It returns the current value of this asset.
• get.SharePrice — The property get method for the dependent SharePrice property returns
the current share price of this stock. For information on how to access web services from
MATLAB, see the webread function.
Bonds Class
The Bonds class represents a specific type of financial asset. It is a concrete class that implements
the abstract members defined by the Assets class and defines class properties and methods specific
to this type of asset.
20-6
A Class Hierarchy for Heterogeneous Arrays
Properties
Methods
• Bonds — The constructor assigns property values and supports a default constructor called with
no input arguments.
• getCurrentVlaue — This method is the Bonds class implementation of the abstract method
defined by the Assets class. It returns the current value of this asset.
• get.CurrentYield — The property get method for the dependent CurrentYield property
returns the current yield on this bond. For information on how to access web serviced from
MATLAB, see the webread function.
20-7
20 Designing Related Classes
end
end
function r = get.CurrentYield(b)
% Implement web access to obtain
% Current yield for this bond
% Returning dummy value
r = 0.24;
end
end
end
Cash Class
The Cash class represents a specific type of financial asset. It is a concrete class that implements the
abstract members defined by the Assets class and defines class properties and methods specific to
this type of asset.
Properties
Methods
• Cash — The constructor assigns property values and supports a default constructor called with no
input arguments.
• getCurrentValue — This method is the Cash class implementation of the abstract method
defined by the Assets class. It returns the current value of this asset.
• save — This method adds the specified amount of cash to the existing amount and returns a new
Cash object with the current amount.
• spend — This method deducts the specified amount from the current amount and returns a new
Cash object with the current amount.
20-8
A Class Hierarchy for Heterogeneous Arrays
c.Description = description;
c.Amount = amount;
end
function value = getCurrentValue(c)
value = c.Amount;
end
function c = save(c,amount)
newValue = c.Amount + amount;
c.Amount = newValue;
end
function c = spend(c,amount)
newValue = c.Amount - amount;
if newValue < 0
c.Amount = 0;
disp('Your balance is $0.00')
else
c.Amount = newValue;
end
end
end
end
Default Object
The design of this class hierarchy uses an abstract root class (Assets). Therefore, the Assets class
must specify a concrete class to use as a default object by overriding getDefaultScalarElement.
In this case, options include:
• Use one of the existing concrete classes for the default object.
• Define a concrete class in the hierarchy to use for the default object.
This implementation adds the DefaultAsset class to the hierarchy as a subclass of the Assets
class. MATLAB creates objects of this class when:
20-9
20 Designing Related Classes
20-10
A Class Hierarchy for Heterogeneous Arrays
end
methods
function obj = DefaultAsset
obj.Description = 'Place holder';
end
function value = getCurrentValue(~)
value = 0;
end
end
end
• pie — Creates a pie chart showing the mix of asset types in the array.
• makeReport — Uses the MATLAB table object to display a table of asset information.
To operate on a heterogeneous array, a method must be defined for the class of the heterogeneous
array and must be sealed. In this case, the class of heterogeneous arrays is always the Assets class.
MATLAB does not use the class of the individual elements of the heterogeneous array when
dispatching to methods.
The Assets class makeReport method builds a table using the common properties and
getCurrentValue method for each object in the array.
function makeReport(obj)
numMembers = length(obj);
descs = cell(1,numMembers);
types(numMembers) = "";
values(numMembers) = 0;
for k = 1:numMembers
descs{k} = obj(k).Description;
types(k) = obj(k).Type;
values(k) = obj(k).getCurrentValue;
end
t = table;
t.Description = descs';
t.Type = types';
t.Value = values';
disp(t)
end
The Assets class pie method calls the getCurrentValue method element-wise on objects in the
array to obtain the data for the pie chart.
function pie(assetArray)
stockAmt = 0; bondAmt = 0; cashAmt = 0;
for k=1:length(assetArray)
if isa(assetArray(k),'financial.Stocks')
stockAmt = stockAmt + assetArray(k).getCurrentValue;
elseif isa(assetArray(k),'financial.Bonds')
20-11
20 Designing Related Classes
Asset class:
+financial/@Assets/Assets.m
+financial/@Assets/makeReport.m
+financial/@Assets/pie.m
Stocks class:
+financial/@Stocks/Stocks.m
Bonds class:
+financial/@Bonds/Bonds.m
Cash class:
+financial/@Cash/Cash.m
DefaultAsset class:
+financial/@DefaultAsset/DefaultAsset.m
These statements create a heterogeneous array by concatenating the Stocks, Bonds, and Cash
objects. Calling the makeReport and pie methods creates the output shown.
s = financial.Stocks('Acme Motor Company',100,string('A'));
b = financial.Bonds('3 Month T',700,0.3);
c(1) = financial.Cash('Bank Account',500);
c(2) = financial.Cash('Gold',500);
20-12
A Class Hierarchy for Heterogeneous Arrays
assetArray = [s,b,c];
makeReport(assetArray)
pie(assetArray)
See Also
Related Examples
• “Designing Heterogeneous Class Hierarchies” on page 10-18
• “Validate Property Values” on page 8-18
• “Get and Set Methods for Dependent Properties” on page 8-42
20-13