MATLAB®
Object-Oriented Programming
R2017a
How to Contact MathWorks
Latest news:
Sales and services:
User community:
Technical support:
Phone:
www.mathworks.com
www.mathworks.com/sales_and_services
www.mathworks.com/matlabcentral
www.mathworks.com/support/contact_us
508-647-7000
The MathWorks, Inc.
3 Apple Hill Drive
Natick, MA 01760-2098
Object-Oriented Programming
© COPYRIGHT 1984–2017 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or
reproduced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation
by, for, or through the federal government of the United States. By accepting delivery of the Program
or Documentation, the government hereby agrees that this software or documentation qualifies as
commercial computer software or commercial computer software documentation as such terms are used
or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and
conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and
govern the use, modification, reproduction, release, performance, display, and disclosure of the Program
and Documentation by the federal government (or other entity acquiring for or through the federal
government) and shall supersede any conflicting contractual terms or conditions. If this License fails
to meet the government's needs or is inconsistent in any respect with federal procurement law, the
government agrees to return the Program and Documentation, unused, to The MathWorks, Inc.
Trademarks
MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand
names may be trademarks or registered trademarks of their respective holders.
Patents
MathWorks products are protected by one or more U.S. patents. Please see
www.mathworks.com/patents for more information.
Revision History
Online only
March 2008
Online only
October 2008
March 2009
Online only
September 2009 Online only
March 2010
Online only
September 2010 Online only
April 2011
Online only
September 2011 Online only
March 2012
Online only
September 2012 Online only
March 2013
Online only
September 2013 Online only
Online only
March 2014
Online only
October 2014
March 2015
Online only
September 2015 Online only
March 2016
Online only
September 2016 Online only
March 2017
Online only
New for MATLAB 7.6 (Release 2008a)
Revised for MATLAB 7.7 (Release 2008b)
Revised for MATLAB 7.8 (Release 2009a)
Revised for MATLAB 7.9 (Release 2009b)
Revised for MATLAB 7.10 (Release 2010a)
Revised for Version 7.11 (Release 2010b)
Revised for Version 7.12 (Release 2011a)
Revised for Version 7.13 (Release 2011b)
Revised for Version 7.14 (Release 2012a)
Revised for Version 8.0 (Release 2012b)
Revised for Version 8.1 (Release 2013a)
Revised for Version 8.2 (Release 2013b)
Revised for Version 8.3 (Release 2014a)
Revised for Version 8.4 (Release 2014b)
Revised for Version 8.5 (Release 2015a)
Revised for Version 8.6 (Release 2015b)
Revised for Version 9.0 (Release 2016a)
Revised for Version 9.1 (Release 2016b)
Revised for Version 9.2 (Release 2017a)
Contents
Using Object-Oriented Design in MATLAB
Why Use Object-Oriented Design . . . . . . . . . . . . . . . . . . . . . .
Approaches to Writing MATLAB Programs . . . . . . . . . . . . . .
When Should You Create Object-Oriented Programs . . . . . . .
Using Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is a Handle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Copies of Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handle Objects Modified in Functions . . . . . . . . . . . . . . . . .
Determine If an Object Is a Handle . . . . . . . . . . . . . . . . . .
Deleted Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-2
1-2
1-6
1-12
1-12
1-12
1-13
1-15
1-16
Basic Example
1
2
Create a Simple Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Design Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Access Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Call Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Add Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Vectorize Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Overload Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
BasicClass Code Listing . . . . . . . . . . . . . . . . . . . . . . . . . . .
2-2
2-2
2-3
2-3
2-4
2-4
2-5
2-6
2-6
v
3
MATLAB Classes Overview
Role of Classes in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . .
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Some Basic Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . .
Developing Classes — Typical Workflow . . . . . . . . . . . . . . . .
Formulating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specifying Class Components . . . . . . . . . . . . . . . . . . . . . . . .
BankAccount Class Implementation . . . . . . . . . . . . . . . . .
Formulating the AccountManager Class . . . . . . . . . . . . . .
Implementing the AccountManager Class . . . . . . . . . . . . .
AccountManager Class Synopsis . . . . . . . . . . . . . . . . . . . .
Using BankAccount Objects . . . . . . . . . . . . . . . . . . . . . . . .
Class to Represent Structured Data . . . . . . . . . . . . . . . . . . .
Objects as Data Structures . . . . . . . . . . . . . . . . . . . . . . . . .
Structure of the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The TensileData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create an Instance and Assign Data . . . . . . . . . . . . . . . . . .
Restrict Properties to Specific Values . . . . . . . . . . . . . . . . .
Simplifying the Interface with a Constructor . . . . . . . . . . . .
Calculate Data on Demand . . . . . . . . . . . . . . . . . . . . . . . . .
Displaying TensileData Objects . . . . . . . . . . . . . . . . . . . .
Method to Plot Stress vs. Strain . . . . . . . . . . . . . . . . . . . . .
TensileData Class Synopsis . . . . . . . . . . . . . . . . . . . . . . .
Class to Implement Linked Lists . . . . . . . . . . . . . . . . . . . . . .
Class Definition Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
dlnode Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Create Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . .
Why a Handle Class for Linked Lists? . . . . . . . . . . . . . . . .
dlnode Class Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Specialize the dlnode Class . . . . . . . . . . . . . . . . . . . . . . . .
Class for Graphing Functions . . . . . . . . . . . . . . . . . . . . . . . .
Class Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the topo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Behavior of the Handle Class . . . . . . . . . . . . . . . . . . . . . . .
3-2
3-2
3-4
3-8
3-8
3-9
3-10
3-14
3-15
3-15
3-16
3-19
3-19
3-19
3-20
3-20
3-21
3-22
3-23
3-24
3-25
3-26
3-31
3-31
3-31
3-32
3-33
3-34
3-45
3-48
3-48
3-49
3-50
vi
Contents
Static Data
Static Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
What Is Static Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Static Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Static Data Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constant Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4-2
4-2
4-2
4-3
4-5
Class Definition—Syntax Reference
4
5
Class Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Definition Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Options for Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . .
Options for Class Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Group Classes with Package Folders . . . . . . . . . . . . . . . . . . .
Class Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Properties Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Events Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Complete Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Related Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Classdef Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
How to Specify Attributes and Superclasses . . . . . . . . . . . .
Class Attribute Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Superclass Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Local Functions in Class File . . . . . . . . . . . . . . . . . . . . . . .
Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Properties Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Access to Property Values . . . . . . . . . . . . . . . . . . . . . . . . . .
5-2
5-2
5-2
5-2
5-3
5-5
5-5
5-5
5-6
5-6
5-7
5-7
5-8
5-9
5-10
5-10
5-10
5-11
5-11
5-12
5-12
5-13
vii
Methods and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Methods Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Method Calling Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Private Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
More Detailed Information On Methods . . . . . . . . . . . . . . .
Class-Related Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .
How to Overload Functions and Operators . . . . . . . . . . . . .
Rules for Defining Methods in Separate Files . . . . . . . . . . .
Events and Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Define and Trigger Events . . . . . . . . . . . . . . . . . . . . . . . . .
Listen for Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attribute Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attribute Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attribute Descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attribute Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simpler Syntax for true/false Attributes . . . . . . . . . . . . . . .
Call Superclass Methods on Subclass Objects . . . . . . . . . . .
Superclass Relation to Subclass . . . . . . . . . . . . . . . . . . . . .
How to Call Superclass Methods . . . . . . . . . . . . . . . . . . . . .
How to Call Superclass Constructor . . . . . . . . . . . . . . . . . .
Representative Class Code . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Calculates Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Description of Class Definition . . . . . . . . . . . . . . . . . . . . . .
MATLAB Code Analyzer Warnings . . . . . . . . . . . . . . . . . . . .
Syntax Warnings and Property Names . . . . . . . . . . . . . . . .
Variable/Property Name Conflict Warnings . . . . . . . . . . . . .
Exception to Variable/Property Name Rule . . . . . . . . . . . . .
Objects In Conditional Statements . . . . . . . . . . . . . . . . . . . .
Enable Use of Objects In Conditional Statements . . . . . . . .
How MATLAB Evaluates Switch Statements . . . . . . . . . . .
How to Define the eq Method . . . . . . . . . . . . . . . . . . . . . . .
Enumerations in Switch Statements . . . . . . . . . . . . . . . . . .
Operations on Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Object Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Help on Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions to Test Objects . . . . . . . . . . . . . . . . . . . . . . . . . .
5-15
5-15
5-15
5-17
5-17
5-17
5-18
5-18
5-20
5-20
5-20
5-22
5-22
5-22
5-23
5-23
5-25
5-25
5-25
5-26
5-28
5-28
5-30
5-33
5-33
5-33
5-34
5-35
5-35
5-35
5-37
5-39
5-42
5-42
5-43
5-45
viii
Contents