100% found this document useful (1 vote)
481 views

Matlab Objectoriented Programming Mathworks instant download

The document provides an overview of MATLAB's Object-Oriented Programming features, including the benefits and design approaches for creating object-oriented programs. It includes detailed sections on creating classes, managing object behavior, and implementing various programming techniques. Additionally, it offers links to related MATLAB resources and guides for further learning.

Uploaded by

galasodhamki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
481 views

Matlab Objectoriented Programming Mathworks instant download

The document provides an overview of MATLAB's Object-Oriented Programming features, including the benefits and design approaches for creating object-oriented programs. It includes detailed sections on creating classes, managing object behavior, and implementing various programming techniques. Additionally, it offers links to related MATLAB resources and guides for further learning.

Uploaded by

galasodhamki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

Matlab Objectoriented Programming Mathworks

download

https://ebookbell.com/product/matlab-objectoriented-programming-
mathworks-49473930

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

A Guide To Matlab Objectoriented Programming Andy H Register

https://ebookbell.com/product/a-guide-to-matlab-objectoriented-
programming-andy-h-register-5249872

Matlab A Practical Introduction To Programming And Problem Solving 6th


Edition Stormy Attaway

https://ebookbell.com/product/matlab-a-practical-introduction-to-
programming-and-problem-solving-6th-edition-stormy-attaway-46075418

Matlab For Engineering Applications 5e Ise 5thise William J Palm Iii

https://ebookbell.com/product/matlab-for-engineering-
applications-5e-ise-5thise-william-j-palm-iii-46562738

Matlab And Simulink Crash Course For Engineers 1st Ed 2022 Eklas
Hossain

https://ebookbell.com/product/matlab-and-simulink-crash-course-for-
engineers-1st-ed-2022-eklas-hossain-46608958
Matlab Medical Imaging Toolbox Users Guide The Mathworks

https://ebookbell.com/product/matlab-medical-imaging-toolbox-users-
guide-the-mathworks-46611260

Matlab For Engineering Applications 5th Edition William Palm

https://ebookbell.com/product/matlab-for-engineering-applications-5th-
edition-william-palm-46651690

Matlab For Engineering Berardino Dacunto

https://ebookbell.com/product/matlab-for-engineering-berardino-
dacunto-48710470

Matlab For Medical Physics Reallife Clinical Scenarios And Projects


Jidi Sun

https://ebookbell.com/product/matlab-for-medical-physics-reallife-
clinical-scenarios-and-projects-jidi-sun-49034592

Matlab For Engineering And The Life Sciences 2nd Edition Joe
Tranquillo

https://ebookbell.com/product/matlab-for-engineering-and-the-life-
sciences-2nd-edition-joe-tranquillo-49437374
MATLAB®
Object-Oriented Programming

R2023a
How to Contact MathWorks

Latest news: www.mathworks.com

Sales and services: www.mathworks.com/sales_and_services

User community: www.mathworks.com/matlabcentral

Technical support: www.mathworks.com/support/contact_us

Phone: 508-647-7000

The MathWorks, Inc.


1 Apple Hill Drive
Natick, MA 01760-2098
Object-Oriented Programming
© COPYRIGHT 1984–2023 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
March 2008 Online only New for MATLAB 7.6 (Release 2008a)
October 2008 Online only Revised for MATLAB 7.7 (Release 2008b)
March 2009 Online only Revised for MATLAB 7.8 (Release 2009a)
September 2009 Online only Revised for MATLAB 7.9 (Release 2009b)
March 2010 Online only Revised for MATLAB 7.10 (Release 2010a)
September 2010 Online only Revised for MATLAB 7.11 (Release 2010b)
April 2011 Online only Revised for MATLAB 7.12 (Release 2011a)
September 2011 Online only Revised for MATLAB 7.13 (Release 2011b)
March 2012 Online only Revised for MATLAB 7.14 (Release 2012a)
September 2012 Online only Revised for MATLAB 8.0 (Release 2012b)
March 2013 Online only Revised for MATLAB 8.1 (Release 2013a)
September 2013 Online only Revised for MATLAB 8.2 (Release 2013b)
March 2014 Online only Revised for MATLAB 8.3 (Release 2014a)
October 2014 Online only Revised for MATLAB 8.4 (Release 2014b)
March 2015 Online only Revised for MATLAB 8.5 (Release 2015a)
September 2015 Online only Revised for MATLAB 8.6 (Release 2015b)
March 2016 Online only Revised for MATLAB 9.0 (Release 2016a)
September 2016 Online only Revised for MATLAB 9.1 (Release 2016b)
March 2017 Online only Revised for MATLAB 9.2 (Release 2017a)
September 2017 Online only Revised for MATLAB 9.3 (Release 2017b)
March 2018 Online only Revised for MATLAB 9.4 (Release 2018a)
September 2018 Online only Revised for MATLAB 9.5 (Release 2018b)
March 2019 Online only Revised for MATLAB 9.6 (Release 2019a)
September 2019 Online only Revised for MATLAB 9.7 (Release 2019b)
March 2020 Online only Revised for MATLAB 9.8 (Release 2020a)
September 2020 Online only Revised for MATLAB 9.9 (Release 2020b)
March 2021 Online only Revised for MATLAB 9.10 (Release 2021a)
September 2021 Online only Revised for MATLAB 9.11 (Release 2021b)
March 2022 Online only Revised for MATLAB 9.12 (Release 2022a)
September 2022 Online only Revised for MATLAB 9.13 (Release 2022b)
March 2023 Online only Revised for MATLAB 9.14 (Release 2023a)
Contents

Using Object-Oriented Design in MATLAB


1
Why Use Object-Oriented Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Approaches to Writing MATLAB Programs . . . . . . . . . . . . . . . . . . . . . . . . 1-2
When Should You Create Object-Oriented Programs . . . . . . . . . . . . . . . . . 1-2

Handle Object Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7


What Is a Handle? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Copies of Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Handle Objects Modified in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Determine If an Object Is a Handle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Deleted Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9

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

MATLAB Classes Overview


3
Role of Classes in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Some Basic Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3

Developing Classes That Work Together . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6


Formulating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Specifying Class Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
BankAccount Class Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Formulating the AccountManager Class . . . . . . . . . . . . . . . . . . . . . . . . . 3-10
Implementing the AccountManager Class . . . . . . . . . . . . . . . . . . . . . . . . 3-11
AccountManager Class Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11

v
Using BankAccount Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12

Representing Structured Data with Classes . . . . . . . . . . . . . . . . . . . . . . . 3-14


Objects as Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Structure of the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
The TensileData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Create an Instance and Assign Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
Restrict Properties to Specific Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
Simplifying the Interface with a Constructor . . . . . . . . . . . . . . . . . . . . . . 3-16
Calculate Data on Demand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
Displaying TensileData Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
Method to Plot Stress vs. Strain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
TensileData Class Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19

Implementing Linked Lists with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23


Class Definition Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23
dlnode Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23
Create Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
Why a Handle Class for Linked Lists? . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25
dlnode Class Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25
Specialize the dlnode Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34

Working with Objects in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37

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

Class Definition—Syntax Reference


5
Components of a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Class Building Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Class Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Properties Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
Methods Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3
Events Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Attribute Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Related Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6

Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7


Methods Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Method Argument Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-8

vi Contents
Special Considerations for Validation in Methods . . . . . . . . . . . . . . . . . . 5-10

Call Superclass Methods on Subclass Objects . . . . . . . . . . . . . . . . . . . . . 5-11


Superclass Relation to Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
How to Call Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
How to Call Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11

Using a Class to Display Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13


Class Calculates Area . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
Description of Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15

MATLAB Code Analyzer Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18


Syntax Warnings and Property Names . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
Variable/Property Name Conflict Warnings . . . . . . . . . . . . . . . . . . . . . . . 5-18
Exception to Variable/Property Name Rule . . . . . . . . . . . . . . . . . . . . . . . 5-19

Objects in Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20


Enable Use of Objects in Conditional Statements . . . . . . . . . . . . . . . . . . 5-20
How MATLAB Evaluates Switch Statements . . . . . . . . . . . . . . . . . . . . . . 5-20
How to Define the eq Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
Enumerations in Switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23

Use of Editor and Debugger with Classes . . . . . . . . . . . . . . . . . . . . . . . . . 5-25


Write Class Code in the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25
How to Refer to Class Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25
How to Debug Class Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25

Automatic Updates for Modified Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27


When MATLAB Loads Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27
Consequences of Automatic Update . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-27
What Happens When Class Definitions Change . . . . . . . . . . . . . . . . . . . . 5-28
Ensure Defining Folder Remains in Scope . . . . . . . . . . . . . . . . . . . . . . . . 5-28
Actions That Do Not Trigger Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
Multiple Updates to Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
Object Validity with Deleted Class File . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
When Updates Are Not Possible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-29
Potential Consequences of Class Updates . . . . . . . . . . . . . . . . . . . . . . . . 5-29
Interactions with the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-30
Updates to Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-30
Updates to Property Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-31
Updates to Method Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-31
Updates to Event Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-32

Comparison of MATLAB and Other OO Languages . . . . . . . . . . . . . . . . . . 5-34


Some Differences from C++ and Java Code . . . . . . . . . . . . . . . . . . . . . . 5-34
Object Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-35
Static Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38
Common Object-Oriented Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38

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

Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5


Specifying Class Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Specifying Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Class-Specific Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7

Functions Inside Class Definition Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-8

Evaluation of Expressions in Class Definitions . . . . . . . . . . . . . . . . . . . . . . 6-9


Why Use Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9
Where to Use Expressions in Class Definitions . . . . . . . . . . . . . . . . . . . . . 6-9
How MATLAB Evaluates Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
When MATLAB Evaluates Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
Expression Evaluation in Handle and Value Classes . . . . . . . . . . . . . . . . 6-11

Folders Containing Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14


Class Definitions on the Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Class and Path Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Using Path Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Using Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15
Functions in Private Folders Within Class Folders . . . . . . . . . . . . . . . . . . 6-15
Class Precedence and MATLAB Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16
Changing Path to Update Class Definition . . . . . . . . . . . . . . . . . . . . . . . . 6-17

Class Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19


Use of Class Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
Why Mark Classes as Inferior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19
InferiorClasses Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-19

Packages Create Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21


Package Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21
Internal Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-21
Referencing Package Members Within Packages . . . . . . . . . . . . . . . . . . . 6-22
Referencing Package Members from Outside the Package . . . . . . . . . . . 6-22
Packages and the MATLAB Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23

Import Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25


Syntax for Importing Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25
Import Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25
Import Package Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25
Package Function and Class Method Name Conflict . . . . . . . . . . . . . . . . 6-26
Clearing Import List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-26

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

Value or Handle Class — Which to Use


7
Comparison of Handle and Value Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
Basic Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
Behavior of MATLAB Built-In Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
User-Defined Value Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3
User-Defined Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4
Determining Equality of Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6
Functionality Supported by Handle Classes . . . . . . . . . . . . . . . . . . . . . . . 7-7

Which Kind of Class to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9


Examples of Value and Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9
When to Use Value Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9
When to Use Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-9

The Handle Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11


Building on the Handle Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
Handle Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
Event and Listener Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11
Relational Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12
Test Handle Validity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12
When MATLAB Destroys Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-12

Handle Class Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-13


Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-13
Syntax of Handle Class Destructor Method . . . . . . . . . . . . . . . . . . . . . . . 7-13
Handle Object During delete Method Execution . . . . . . . . . . . . . . . . . . . 7-14
Support Destruction of Partially Constructed Objects . . . . . . . . . . . . . . . 7-15
When to Define a Destructor Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15
Destructors in Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Object Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Restrict Access to Object Delete Method . . . . . . . . . . . . . . . . . . . . . . . . . 7-17
Nondestructor Delete Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18
External References to MATLAB Objects . . . . . . . . . . . . . . . . . . . . . . . . . 7-18

Find Handle Objects and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21


Find Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21
Find Handle Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-21

Implement Set/Get Interface for Properties . . . . . . . . . . . . . . . . . . . . . . . 7-22


The Standard Set/Get Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Subclass Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Get Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Set Method Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-23
Class Derived from matlab.mixin.SetGet . . . . . . . . . . . . . . . . . . . . . . . . . 7-24
Set Priority for Matching Partial Property Names . . . . . . . . . . . . . . . . . . 7-27

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

Properties — Storing Class Data


8
Ways to Use Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
What Are Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2
Types of Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2

Property Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4


Property Definition Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4
Property Validation Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-5
Property Access Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6

Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8


Purpose of Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8
Specify Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8
Table of Property Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-8
Property Access Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12

Initialize Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13


Define Properties with Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13
Set Property Values in the Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14

Mutable and Immutable Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16


Set Access to Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16
Define Immutable Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-16

Validate Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-18


Property Validation in Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 8-18
Sample Class Using Property Validation . . . . . . . . . . . . . . . . . . . . . . . . . 8-19
Order of Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-20
Abstract Property Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-21
Objects Not Updated When Changing Validation . . . . . . . . . . . . . . . . . . . 8-21
Validation During Load Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-21

Property Class and Size Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-23


Property Class and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-23
Property Size Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-23
Property Class Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-24
Default Values Per Size and Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-28

Property Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-29


MATLAB Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-29
Validate Property Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-31
Define Validation Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-34

Metadata Interface to Property Validation . . . . . . . . . . . . . . . . . . . . . . . . 8-36

x Contents
Property Get and Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-38
Property Get Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-38
Property Set Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-39

Get and Set Methods for Dependent Properties . . . . . . . . . . . . . . . . . . . . 8-42


Define a Get Method for a Dependent Property . . . . . . . . . . . . . . . . . . . . 8-42
When to Use Set Methods with Dependent Properties . . . . . . . . . . . . . . . 8-43

Properties Containing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-45


Assigning Objects as Default Property Values . . . . . . . . . . . . . . . . . . . . . 8-45
Assigning to Read-Only Properties Containing Objects . . . . . . . . . . . . . . 8-45
Assignment Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-45

Dynamic Properties — Adding Properties to an Instance . . . . . . . . . . . . 8-47


What Are Dynamic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-47
Define Dynamic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-47
List Object Dynamic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-49

Set and Get Methods for Dynamic Properties . . . . . . . . . . . . . . . . . . . . . . 8-51


Create Access Methods for Dynamic Properties . . . . . . . . . . . . . . . . . . . 8-51

Dynamic Property Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-53


Dynamic Properties and Ordinary Property Events . . . . . . . . . . . . . . . . . 8-53
Dynamic-Property Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-53
Listen for a Specific Property Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-54
PropertyAdded Event Callback Execution . . . . . . . . . . . . . . . . . . . . . . . . 8-55
PropertyRemoved Event Callback Execution . . . . . . . . . . . . . . . . . . . . . . 8-55
How to Find meta.DynamicProperty Objects . . . . . . . . . . . . . . . . . . . . . . 8-55

Dynamic Properties and ConstructOnLoad . . . . . . . . . . . . . . . . . . . . . . . . 8-57

Methods — Defining Class Operations


9
Methods in Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Examples and Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Kinds of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2
Method Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-3

Method Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4


Purpose of Method Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
Specifying Method Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4
Table of Method Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4

Ordinary Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6


Ordinary Methods Operate on Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6
Methods Inside classdef Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6
Method Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-7

Methods in Separate Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8


Class Folders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8

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

Method Invocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11


Dot and Function Syntaxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-11
Determining Which Method Is Invoked . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12

Class Constructor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15


Purpose of Class Constructor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 9-15
Basic Structure of Constructor Methods . . . . . . . . . . . . . . . . . . . . . . . . . 9-15
Guidelines for Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16
Default Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
When to Define Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
Related Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
Initializing Objects in Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17
No Input Argument Constructor Requirement . . . . . . . . . . . . . . . . . . . . . 9-18
Subclass Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-18
Implicit Call to Inherited Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21
Errors During Class Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21
Output Object Suppressed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-22

Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23


What Are Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23
Why Define Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23
Defining Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23
Calling Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-23
Inheriting Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-24

Overload Functions in Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . 9-25


Why Overload Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-25
Implementing Overloaded MATLAB Functions . . . . . . . . . . . . . . . . . . . . 9-25
Rules for Naming to Avoid Conflicts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-27

Class Support for Array-Creation Functions . . . . . . . . . . . . . . . . . . . . . . . 9-28


Extend Array-Creation Functions for Your Class . . . . . . . . . . . . . . . . . . . 9-28
Which Syntax to Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-29
Implement Support for Array-Creation Functions . . . . . . . . . . . . . . . . . . 9-30

Class Methods for Graphics Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-35


Referencing the Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-35
Syntax for Method Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-35
Use a Class Method for a Slider Callback . . . . . . . . . . . . . . . . . . . . . . . . 9-36

Object Arrays
10
Construct Object Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Build Arrays in the Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2
Referencing Property Values in Object Arrays . . . . . . . . . . . . . . . . . . . . . 10-2

xii Contents
Initialize Object Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-5
Calls to Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-5
Initial Value of Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-6

Empty Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7


Creating Empty Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7
Assigning Values to an Empty Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-7

Initialize Arrays of Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9


Related Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10

Accessing Dynamic Properties in Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 10-11

Implicit Class Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13


Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13
Subscripted Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13
Property Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-14
Function and Method Argument Validation . . . . . . . . . . . . . . . . . . . . . . 10-15

Concatenating Objects of Different Classes . . . . . . . . . . . . . . . . . . . . . . 10-17


Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17
MATLAB Concatenation Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17
Concatenating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17
Calling the Dominant-Class Constructor . . . . . . . . . . . . . . . . . . . . . . . . 10-18
Converter Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19

Designing Heterogeneous Class Hierarchies . . . . . . . . . . . . . . . . . . . . . 10-22


Creating Classes That Support Heterogeneous Arrays . . . . . . . . . . . . . 10-22
MATLAB Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22
Heterogeneous Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22
Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23
Heterogeneous Array Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23
Nature of Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24
Unsupported Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26
Default Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-27
Conversion During Assignment and Concatenation . . . . . . . . . . . . . . . . 10-28
Empty Arrays of Heterogeneous Abstract Classes . . . . . . . . . . . . . . . . . 10-28

Heterogeneous Array Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29


Building Arrays in Superclass Constructors . . . . . . . . . . . . . . . . . . . . . 10-29
When Errors Can Occur . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29
Initialize Array in Superclass Constructor . . . . . . . . . . . . . . . . . . . . . . . 10-29
Sample Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-30
Potential Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-32

Events — Sending and Responding to Messages


11
Overview Events and Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Why Use Events and Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Events and Listeners Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2
Event Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-2

xiii
Create Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3

Define Custom Event Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5


Class Event Data Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
Define and Trigger Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5
Define Event Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-6
Create Listener for Overflow Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-6

Observe Changes to Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-8

Implement Property Set Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10


PushButton Class Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-10

Event and Listener Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12


The Event Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12
Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13
Default Event Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-13
Events Only in Handle Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-14
Property-Set and Query Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-14
Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-15

Event Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-16


Specify Event Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-16

Events and Listeners Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18


Components to Implement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
Name Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
Trigger Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-18
Listen to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-19
Define Event-Specific Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-21

Listener Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23


Control Listener Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23
Temporarily Deactivate Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23
Permanently Delete Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-23

Listener Callback Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-24


Specifying Listener Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-24
Input Arguments for Callback Function . . . . . . . . . . . . . . . . . . . . . . . . 11-24
Additional Arguments for Callback Function . . . . . . . . . . . . . . . . . . . . . 11-25

Callback Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27


When Callbacks Execute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
Listener Order of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
Callbacks That Call notify . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
Manage Callback Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-27
Invoke Functions from Function Handles . . . . . . . . . . . . . . . . . . . . . . . 11-27

Determine If Event Has Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29


Do Listeners Exist for This Event? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29
Why Test for Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29
Coding Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29
Listeners in Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-29

xiv Contents
Listen for Changes to Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . 11-32
Create Property Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-32
Property Event and Listener Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-33

Assignment When Property Value Is Unchanged . . . . . . . . . . . . . . . . . . 11-35


AbortSet When Value Does Not Change . . . . . . . . . . . . . . . . . . . . . . . . 11-35
How MATLAB Compares Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-35
When to Use AbortSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-35
Implement AbortSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-36
Using AbortSet with Property Validation . . . . . . . . . . . . . . . . . . . . . . . . 11-37

Techniques for Using Events and Listeners . . . . . . . . . . . . . . . . . . . . . . 11-40


Example Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-40
Techniques Demonstrated in This Example . . . . . . . . . . . . . . . . . . . . . . 11-41
Summary of fcneval Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-41
Summary of fcnview Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-42
Methods Inherited from Handle Class . . . . . . . . . . . . . . . . . . . . . . . . . . 11-43
Using the fcneval and fcnview Classes . . . . . . . . . . . . . . . . . . . . . . . . . 11-43
Implement UpdateGraph Event and Listener . . . . . . . . . . . . . . . . . . . . 11-45
The PostSet Event Listener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-48
Enable and Disable Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-50
@fcneval/fcneval.m Class Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-51
@fcnview/fcnview.m Class Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-52

How to Build on Other Classes


12
Hierarchies of Classes — Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Classification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-2
Develop the Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Design of Class Hierarchies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Super and Subclass Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-3
Implementation and Interface Inheritance . . . . . . . . . . . . . . . . . . . . . . . 12-4

Subclass Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-5


Subclass Definition Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-5
Subclass double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-5

Design Subclass Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7


Call Superclass Constructor Explicitly . . . . . . . . . . . . . . . . . . . . . . . . . . 12-7
Call Superclass Constructor from Subclass . . . . . . . . . . . . . . . . . . . . . . . 12-7
Subclass Constructor Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-8
Call Only Direct Superclass from Constructor . . . . . . . . . . . . . . . . . . . . . 12-9

Control Sequence of Constructor Calls . . . . . . . . . . . . . . . . . . . . . . . . . . 12-11

Modify Inherited Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13


When to Modify Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
Extend Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-13
Reimplement Superclass Process in Subclass . . . . . . . . . . . . . . . . . . . . 12-14
Redefine Superclass Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-15
Implement Abstract Method in Subclass . . . . . . . . . . . . . . . . . . . . . . . . 12-15

xv
Modify Inherited Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17
Superclass Property Modification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-17
Private Local Property Takes Precedence in Method . . . . . . . . . . . . . . . 12-17

Subclassing Multiple Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-19


Specify Multiple Superclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-19
Class Member Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-19
Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-20

Specify Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21


Why Control Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
Specify Allowed Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-21
Define Sealed Hierarchy of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-22

Class Members Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-23


Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-23
Applications for Access Control Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 12-24
Specify Access to Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-24
Properties with Access Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25
Methods with Access Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-25
Abstract Methods with Access Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-28

Method Access List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-29

Event Access List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-30

Handle Compatible Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-31


Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-31
When to Use Handle-Compatible Classes . . . . . . . . . . . . . . . . . . . . . . . 12-31
Handle Compatibility Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-31
Identify Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-32

How to Define Handle-Compatible Classes . . . . . . . . . . . . . . . . . . . . . . . 12-33


What Is Handle Compatibility? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-33
Subclassing Handle-Compatible Classes . . . . . . . . . . . . . . . . . . . . . . . . 12-35

Methods for Handle-Compatible Classes . . . . . . . . . . . . . . . . . . . . . . . . . 12-37


Methods for Handle and Value Objects . . . . . . . . . . . . . . . . . . . . . . . . . 12-37
Modify Value Objects in Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-37

Handle-Compatible Classes and Heterogeneous Arrays . . . . . . . . . . . . 12-38


Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-38
Methods Must Be Sealed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-38
Template Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-38

Subclasses of MATLAB Built-In Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-40


MATLAB Built-In Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-40
Built-In Types You Can Subclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-40
Why Subclass Built-In Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-40
Which Functions Work with Subclasses of Built-In Types . . . . . . . . . . . 12-41
Behavior of Built-In Functions with Subclass Objects . . . . . . . . . . . . . . 12-41
Built-In Subclasses That Define Properties . . . . . . . . . . . . . . . . . . . . . . 12-42

Behavior of Inherited Built-In Methods . . . . . . . . . . . . . . . . . . . . . . . . . 12-43


Subclass double . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-43

xvi Contents
Built-In Data Value Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-44
Built-In Data Organization Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-44
Built-In Indexing Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-45
Built-In Concatenation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-45

Subclasses of Built-In Types Without Properties . . . . . . . . . . . . . . . . . . 12-47


Specialized Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-47
A Class to Manage uint8 Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-47
Using the DocUint8 Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-48

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

Use of size and numel with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-60


size and numel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-60
Built-In Class Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-60
Subclasses Inherit Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-61
Classes Not Derived from Built-In Classes . . . . . . . . . . . . . . . . . . . . . . 12-62
Change the Behavior of size or numel . . . . . . . . . . . . . . . . . . . . . . . . . . 12-63
Overload numArgumentsFromSubscript Instead of numel . . . . . . . . . . . 12-64

Determine Array Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-65


Query the Class Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-65
Test for Array Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-65
Test for Specific Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-66
Test for Most Derived Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-66

Abstract Classes and Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-68


Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-68
Declare Classes as Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-68
Determine If a Class Is Abstract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-70
Find Inherited Abstract Properties and Methods . . . . . . . . . . . . . . . . . . 12-70

Define an Interface Superclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-72


Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-72
Interface Class Implementing Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 12-72

Saving and Loading Objects


13
Save and Load Process for Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Save and Load Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
What Information Is Saved? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
How Is the Property Data Loaded? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-2
Errors During Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-3

xvii
Reduce MAT-File Size for Saved Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Dependent Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Transient Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4
Avoid Saving Unwanted Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4

Save Object Data to Recreate Graphics Objects . . . . . . . . . . . . . . . . . . . . 13-5


What to Save . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5
Regenerate When Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-5
Change to a Stairstep Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-6

Improve Version Compatibility with Default Values . . . . . . . . . . . . . . . . . 13-7


Version Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7
Using a Default Property Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-7

Avoid Property Initialization Order Dependency . . . . . . . . . . . . . . . . . . . 13-9


Control Property Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-9
Dependent Property with Private Storage . . . . . . . . . . . . . . . . . . . . . . . . 13-9
Property Value Computed from Other Properties . . . . . . . . . . . . . . . . . 13-11

Modify the Save and Load Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-12


When to Modify the Save and Load Process . . . . . . . . . . . . . . . . . . . . . 13-12
How to Modify the Save and Load Process . . . . . . . . . . . . . . . . . . . . . . 13-12
Implementing saveobj and loadobj Methods . . . . . . . . . . . . . . . . . . . . . 13-12
Additional Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-13

Basic saveobj and loadobj Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-14


Using saveobj and loadobj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-14
Handle Load Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-15

Maintain Class Compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-17


Rename Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-17
Update Property When Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-18
Maintaining Compatible Versions of a Class . . . . . . . . . . . . . . . . . . . . . 13-19
Version 2 of the PhoneBookEntry Class . . . . . . . . . . . . . . . . . . . . . . . . . 13-20

Initialize Objects When Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-22


Calling Constructor When Loading Objects . . . . . . . . . . . . . . . . . . . . . . 13-22
Initializing Objects in the loadobj Method . . . . . . . . . . . . . . . . . . . . . . . 13-22

Save and Load Objects from Class Hierarchies . . . . . . . . . . . . . . . . . . . . 13-24


Saving and Loading Subclass Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 13-24
Reconstruct the Subclass Object from a Saved struct . . . . . . . . . . . . . . 13-24

Restore Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-26


Create Listener with loadobj . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-26
Use Transient Property to Load Listener . . . . . . . . . . . . . . . . . . . . . . . . 13-26
Using the BankAccount and AccountManager Classes . . . . . . . . . . . . . 13-27

xviii Contents
Enumerations
14
Named Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Kinds of Predefined Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2
Techniques for Defining Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . 14-2

Define Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4


Enumeration Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4
Construct an Enumeration Member . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4
Convert to Superclass Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-4
Define Methods in Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . 14-5
Define Properties in Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . 14-6
Enumeration Class Constructor Calling Sequence . . . . . . . . . . . . . . . . . . 14-7

Refer to Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9


Instances of Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-9
Conversion of Characters to Enumerations . . . . . . . . . . . . . . . . . . . . . . 14-10
Enumeration Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-12

Enumerations for Property Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-14


Syntax for Property/Enumeration Definition . . . . . . . . . . . . . . . . . . . . . 14-14
Example of Restricted Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-14

Operations on Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16


Operations Supported by Enumerations . . . . . . . . . . . . . . . . . . . . . . . . 14-16
Example Enumeration Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16
Default Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-16
Convert Enumeration Members to Strings or char Vectors . . . . . . . . . . 14-17
Convert Enumeration Arrays to String Arrays or Cell Arrays of char Vectors
.................................................... 14-17
Relational Operations with Enumerations, Strings, and char Vectors . . . 14-18
Enumerations in switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-19
Enumeration Set Membership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-20
Enumeration Text Comparison Methods . . . . . . . . . . . . . . . . . . . . . . . . 14-21
Get Information About Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . 14-21
Testing for an Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-22

Hide Enumeration Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-23


Hide Pure Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-24
Find Hidden Enumeration Members . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-24

Enumeration Class Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-26

Enumerations Derived from Built-In Classes . . . . . . . . . . . . . . . . . . . . . 14-27


Subclassing Built-In Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-27
Derive Enumeration Class from Numeric Class . . . . . . . . . . . . . . . . . . . 14-27
How to Alias Enumeration Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-28
Superclass Constructor Returns Underlying Value . . . . . . . . . . . . . . . . 14-29
Default Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-30

Mutable Handle vs. Immutable Value Enumeration Members . . . . . . . 14-32


Select Handle- or Value-Based Enumerations . . . . . . . . . . . . . . . . . . . . 14-32
Value-Based Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-32

xix
Handle-Based Enumeration Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-33
Represent State with Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-35

Enumerations That Encapsulate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-37


Enumeration Classes with Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 14-37
Store Data in Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-37

Save and Load Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-40


Basic Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-40
Built-In and Value-Based Enumeration Classes . . . . . . . . . . . . . . . . . . . 14-40
Simple and Handle-Based Enumeration Classes . . . . . . . . . . . . . . . . . . 14-40
Causes: Load as struct Instead of Object . . . . . . . . . . . . . . . . . . . . . . . . 14-40

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

Information from Class Metadata


16
Class Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
What Is Class Metadata? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
The meta Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-2
Metaclass Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-3
Metaclass Object Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-3

Class Introspection with Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-5


Using Class Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-5
Inspect the EmployeeData Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-5
Metaclass EnumeratedValues Property . . . . . . . . . . . . . . . . . . . . . . . . . . 16-7

Find Objects with Specific Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-9


Find Handle Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-9
Find by Attribute Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-10

Get Information About Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-12


The meta.property Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-12
How to Find Properties with Specific Attributes . . . . . . . . . . . . . . . . . . 16-14

Find Default Values in Property Metadata . . . . . . . . . . . . . . . . . . . . . . . 16-17


Default Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-17
meta.property Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-17

xx Contents
Specialize Object Behavior
17
Methods That Modify Default Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
How to Customize Class Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Which Methods Control Which Behaviors . . . . . . . . . . . . . . . . . . . . . . . . 17-2
Overload Functions and Override Methods . . . . . . . . . . . . . . . . . . . . . . . 17-3

Concatenation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-4


Default Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-4
Methods to Overload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-4

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

Customize Object Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-7


Default Object Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-7
Customize Object Indexing With Modular Indexing Classes . . . . . . . . . . . 17-8

Code Patterns for subsref and subsasgn Methods . . . . . . . . . . . . . . . . . . 17-9


Customize Indexed Reference and Assignment . . . . . . . . . . . . . . . . . . . . 17-9
Syntax for subsref and subsasgn Methods . . . . . . . . . . . . . . . . . . . . . . . . 17-9
Indexing Structure Describes Indexing Expressions . . . . . . . . . . . . . . . . 17-9
Values of the Indexing Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-10
Typical Patterns for Indexing Methods . . . . . . . . . . . . . . . . . . . . . . . . . 17-11

Overload end for Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-15


Syntax and Default Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-15
How RedefinesParen Overloads end . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-15

Objects in Index Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-17


Objects as Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-17
Ways to Implement Objects as Indices . . . . . . . . . . . . . . . . . . . . . . . . . 17-17
subsindex Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-17

Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-19


Why Overload Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-19
How to Define Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-19
Sample Implementation — Addable Objects . . . . . . . . . . . . . . . . . . . . . 17-20
MATLAB Operators and Associated Functions . . . . . . . . . . . . . . . . . . . . 17-21

Customize Parentheses Indexing for Mapping Class . . . . . . . . . . . . . . . 17-23

Forward Indexing Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17-30

xxi
Customizing Object Display
18
Custom Display Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
Command Window Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
Default Object Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-2
CustomDisplay Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-3
Methods for Customizing Object Display . . . . . . . . . . . . . . . . . . . . . . . . . 18-3

How CustomDisplay Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-7


Steps to Display an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-7
Methods Called for a Given Object State . . . . . . . . . . . . . . . . . . . . . . . . . 18-7

Role of size Function in Custom Displays . . . . . . . . . . . . . . . . . . . . . . . . . 18-9


How size Is Used . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-9
Precautions When Overloading size . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-9

Customize Display for Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . 18-10

Class with Default Object Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-11


The EmployeeInfo Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-11
Default Display — Scalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-11
Default Display — Nonscalar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-12
Default Display — Empty Object Array . . . . . . . . . . . . . . . . . . . . . . . . . 18-12
Default Display — Handle to Deleted Object . . . . . . . . . . . . . . . . . . . . . 18-13
Default Display — Detailed Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-13

Choose a Technique for Display Customization . . . . . . . . . . . . . . . . . . . 18-15


Ways to Implement a Custom Display . . . . . . . . . . . . . . . . . . . . . . . . . . 18-15
Sample Approaches Using the Interface . . . . . . . . . . . . . . . . . . . . . . . . 18-15

Customize Property Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-18


Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Change the Property Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-18
Change the Values Displayed for Properties . . . . . . . . . . . . . . . . . . . . . 18-18

Customize Header, Property List, and Footer . . . . . . . . . . . . . . . . . . . . . 18-21


Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-21
Design of Custom Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-21
getHeader Method Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-22
getPropertyGroups Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-23
getFooter Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-23

Customize Display of Scalar Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-26


Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-26
Design of Custom Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-26
displayScalarObject Method Override . . . . . . . . . . . . . . . . . . . . . . . . . . 18-27
getPropertyGroups Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-27

Customize Display of Object Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-30


Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-30
Design of Custom Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-30
The displayNonScalarObject Override . . . . . . . . . . . . . . . . . . . . . . . . . 18-31
The displayEmptyObject Override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-32

xxii Contents
Overloading the disp Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-34
Display Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-34
Overloaded disp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-34
Relationship Between disp and display . . . . . . . . . . . . . . . . . . . . . . . . . 18-34

Custom Compact Display Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18-36


Customization Options Available for Compact Display . . . . . . . . . . . . . . 18-36
Designing a Class with a Customized Compact Display . . . . . . . . . . . . . 18-36

Defining Custom Data Types


19
Representing Polynomials with Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2
Class Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19-2
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

Designing Related Classes


20
A Class Hierarchy for Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . 20-2
Interfaces Based on Heterogeneous Arrays . . . . . . . . . . . . . . . . . . . . . . . 20-2
Define Heterogeneous Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-2
Assets Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-4
Stocks Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-5
Bonds Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-6
Cash Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-8
Default Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-9
Operating on an Assets Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20-11

xxiii
1

Using Object-Oriented Design in


MATLAB

• “Why Use Object-Oriented Design” on page 1-2


• “Handle Object Behavior” on page 1-7
1 Using Object-Oriented Design in MATLAB

Why Use Object-Oriented Design


In this section...
“Approaches to Writing MATLAB Programs” on page 1-2
“When Should You Create Object-Oriented Programs” on page 1-2

Approaches to Writing MATLAB Programs


Creating software applications typically involves designing the application data and implementing
operations performed on that data. Procedural programs pass data to functions, which perform the
necessary operations on the data. Object-oriented software encapsulates data and operations in
objects that interact with each other via the object's interface.

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.

Procedural Program Design

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.

Object-Oriented Program Design

The object-oriented program design involves:

• 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.

Classes and Objects

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.

When Should You Create Object-Oriented Programs


You can implement simple programming tasks as simple functions. However, as the magnitude and
complexity of your tasks increase, functions become more complex and difficult to manage.

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

Understand a Problem in Terms of Its Objects

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 Manage Internal State

Objects provide several useful features not available from structures and cell arrays. For example,
objects can:

• Constrain the data values assigned to any given property


• Calculate the value of a property only when it is queried
• Broadcast notices when any property value is queried or changed
• Restrict access to properties and methods

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.

Defining Consistent Interfaces

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:

• Identify the requirements of a particular objective


• Encode requirements into your program as an interface class

Reducing Complexity

Objects reduce complexity by reducing what you must know to use a component or system:

• Objects provide an interface that hides implementation details.


• Objects enforce rules that control how objects interact.

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.

Here is a diagram of a three-element list:

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:

First disconnect the nodes:

1 Unlink n2.Prev from n1


2 Unlink n1.Next from n2

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:

• Implemented in an optimal way by the class author


• Always up to date with the current version of the class
• Properly tested
• Can automatically update old-versions of the objects when they are loaded from MAT-files.

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.

Overloaded Functions and Operators

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

Handle Object Behavior


In this section...
“What Is a Handle?” on page 1-7
“Copies of Handles” on page 1-7
“Handle Objects Modified in Functions” on page 1-8
“Determine If an Object Is a Handle” on page 1-9
“Deleted Handle Objects” on page 1-9

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.

For more information on handle classes, see “Handle Classes”.

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;

Creates another variable, h2, that refers to the same object as 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;

You can use gongSound2 to access the same audio source:

disp(gongSound2.SampleRate)

16384

Play the gong sound with the new sample rate:

play(gongSound2)

Handle Objects Modified in Functions


When you pass an argument to a function, the function copies the variable from the workspace in
which you call the function into the parameter variable in the function’s workspace.

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

Define a variable and pass it to myfunc:

x = 12;
myFunc(x)

The value of x has not changed after executing 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.

function out = myFunc(var)


out = var + 1;
end

Modify a value in myfunc:

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

Create an audioplayer object and pass it to the modifySampleRate function:


load gong Fs y
gongSound = audioplayer(y,Fs);
disp(gongSound.SampleRate)

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.

Determine If an Object Is a Handle


Handle objects are members of the handle class. Therefore, you can always identify an object as a
handle using the isa function. isa returns logical true (1) when testing for a handle variable:
load gong Fs y
gongSound = audioplayer(y,Fs);
isa(gongSound,'handle')

To determine if a variable is a valid handle object, use isa and isvalid:


if isa(gongSound,'handle') && isvalid(gongSound)
...
end

Deleted 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.

For example, create an audioplayer object:


load gong Fs y
gongSound = audioplayer(y,Fs);

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)

However, the handle variable still exists:

disp(gongSound)

handle to deleted audioplayer

The whos command shows gongSound as an audioplayer object:

whos

Name Size Bytes Class Attributes

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.

You cannot access properties with the invalid handle variable:

gongSound.SampleRate

Invalid or deleted object.

Functions and methods that access object properties cause an error:

play(gongSound)

Invalid or deleted object.

To remove the variable, gongSound, use clear:

clear gongSound
whos

Name Size Bytes Class Attributes

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

Creating a Simple Class

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

For a summary of class syntax, see classdef.

To use the class:

• 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 =

BasicClass with properties:

Value: []

Initially, the property value is empty.

Access Properties
Assign a value to the Value property using the object variable and a dot before the property name:

a.Value = pi/3;

To return a property value, use dot notation without the assignment:

a.Value

ans =

1.0472

For information on class properties, see “Property Syntax” on page 8-4.

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

You can also call a method using dot notation:

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.

For information on class methods, see “Method Syntax” on page 5-7.

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 =

BasicClass with properties:

Value: 1.0472

The constructor can perform other operations related to creating objects of the class.

For information on constructors, see “Class Constructor Methods” on page 9-15.

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);

These two expressions are equivalent.

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);

Because roundOff is vectorized, it can operate on arrays.

roundOff(obj)

ans =

2.8000 0.8700 7.0000

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

By vectorizing the plus method, you can operate on object arrays.

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.

For information on overloading operators, see “Operator Overloading” on page 17-19.

2-5
2 Basic Example

BasicClass Code Listing


Here is the BasicClass definition after adding the features discussed in this topic:

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

MATLAB Classes Overview

• “Role of Classes in MATLAB” on page 3-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
• “Working with Objects in MATLAB” on page 3-37
3 MATLAB Classes Overview

Role of Classes in MATLAB


In this section...
“Classes” on page 3-2
“Some Basic Relationships” on page 3-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 — Key Terms

MATLAB classes use the following words to describe different parts of a class definition and related
concepts.

• Class definition — Description of what is common to every instance of a class.


• Properties — Data storage for class instances
• Methods — Special functions that implement operations that are usually performed only on
instances of the class
• Events — Messages defined by classes and broadcast by class instances when some specific action
occurs
• Attributes — Values that modify the behavior of properties, methods, events, and classes
• Listeners — Objects that respond to a specific event by executing a callback function when the
event notice is broadcast
• Objects — Instances of classes, which contain actual data values stored in the objects' properties
• Subclasses — Classes that are derived from other classes and that inherit the methods, properties,
and events from those classes (subclasses facilitate the reuse of code defined in the superclass
from which they are derived).
• Superclasses — Classes that are used as a basis for the creation of more specifically defined
classes (that is, subclasses).
• Packages — Folders that define a scope for class and function naming

Some Basic Relationships


This section discusses some of the basic concepts used by MATLAB classes.

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:

• A Positive Integer is an Integer


• A Positive Integer is a Positive number

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

Developing Classes That Work Together


In this section...
“Formulating a Class” on page 3-6
“Specifying Class Components” on page 3-7
“BankAccount Class Implementation” on page 3-7
“Formulating the AccountManager Class” on page 3-10
“Implementing the AccountManager Class” on page 3-11
“AccountManager Class Synopsis” on page 3-11
“Using BankAccount Objects” on page 3-12

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.)

You must perform certain operations on a bank account:

• Create an object for each bank account


• Deposit money
• Withdraw money
• Generate a statement
• Save and load the BankAccount object

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:

• open — Account balance is a positive value


• overdrawn — Account balance is overdrawn, but by $200 or less.
• closed — Account balance is overdrawn by more than $200.

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

Specifying Class Components


Classes store data in properties, implement operations with methods, and support notifications with
events and listeners. Here is how the BankAccount and AccountManager classes define these
components.

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

These methods implement the operations defined in the class formulation:

• 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.

BankAccount Class Implementation


It is important to ensure that there is only one set of data associated with any object of a
BankAccount class. You would not want independent copies of the object that could have, for

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.

BankAccount Class Synopsis

BankAccount Class Discussion


classdef BankAccount < handle Handle class because there should be only one
copy of any instance of BankAccount.
“Comparison of Handle and Value Classes” on
page 7-2
properties (Access = ?AccountManager) AccountStatus contains the status of the
AccountStatus = 'open' account determined by the current balance.
end Access is limited to the BankAccount and
AccountManager classes. “Class Members
Access” on page 12-23
properties (SetAccess = private) AccountStatus property access by
AccountNumber
AccountBalance AccountManager class methods.
end
properties (Transient) AccountNumber and AccountBalance
AccountListener
end properties have private set access.

AccountListener property is transient so


the listener handle is not saved.

See “Property Attributes” on page 8-8.


events Class defines event called
InsufficientFunds
end InsufficentFunds. withdraw method
triggers event when account balance becomes
negative.

For information on events and listeners, see


“Events” .
methods Block of ordinary methods. See “Method
Syntax” on page 5-7 for syntax.
Constructor initializes property values with
function BA = BankAccount(AccountNumber,InitialBalance)
BA.AccountNumber = AccountNumber;
BA.AccountBalance = InitialBalance; input arguments.
BA.AccountListener = AccountManager.addAccount(BA);
end AccountManager.addAccount is static
method of AccountManager class. Creates
listener for InsufficientFunds event and
stores listener handle in AccountListener
property.
function deposit(BA,amt) deposit adjusts value of AccountBalance
BA.AccountBalance = BA.AccountBalance + amt;
if BA.AccountBalance > 0 property.
BA.AccountStatus = 'open';
end If AccountStatus is closed and subsequent
end
deposit brings AccountBalance into positive
range, then AccountStatus is reset to open.

3-8
Random documents with unrelated
content Scribd suggests to you:
the band played the Star-spangled Banner. All this was done without
sound of any sort, save the sweet strains of the music, or the deeper
drawing of the breath, and yet the men of other nations uncovered
their heads in respectful acknowledgment of the fact that they stood
before the representatives of the truest and most patriotic country
on earth.

So many things crowd to the place where the gray matter should
be that I gasp for breath. I wonder if every woman who comes over
here is possessed with the wild desire to write letters. I go to places
now, that I may tell you about them, and am uneasy until I reach
my little sky-parlor in order to begin the telling.

Can I ever make you understand how much, how very much, I
appreciate all the delights you are making it possible for me to
enjoy? Were I to be stricken blind and deaf, and then live a
thousand years, I have enough of beauty of color, of sound and of
fragrance to enable me to live happily through it all. And yet, I am
going to say, "I told you so."

You never did so unwise a thing as to induce me to bring those


trunks. We have discarded them, and have each purchased an
English "hold-all" and a dress basket. This last we send to the place
where we are to be at the week's end, and there we are laundered,
and away it goes to our next resting-place.

I find that one can get her linen washed quickly, cheaply and
well in all parts of England. You give your soiled clothes, with a
thru'pence, to your maid at night, and you will find them at your
door, along with your shoes, in the morning—shoes and all having
been thoroughly washed.
There is a system of "carted luggage" here by which one may
send any large piece of luggage that can be locked (it will not be
taken otherwise) from one's door and find it in one's room at the
hotel or lodgings in the next city. The cost is nominal. Unless one
comes to visit or for social duties, only the bare necessities should
be taken. Other articles are an extra bother and expense. We have
learned, too, to write in advance, in time for a reply, before
venturing to hotels or lodgings. Women unaccompanied by men do
not receive the best attention in Europe unless "expected."

FRESHWATER, ISLE OF WIGHT:

In coming to the Isle of Wight we journeyed from London to


Portsmouth by rail, and from Portsmouth to Ryde by boat across
the Solent. The Spithead, as this part of the Solent is called, is the
naval rendezvous of the world. Portsmouth harbor is filled with
historic interest. It is here that Nelson's famous flagship Victory, now
a schoolship, is anchored. Off to the northward are many basins
lined with factories. A monstrous floating bridge carries multitudes of
passengers and vehicles, and the smaller ferries and boats of every
description make a wonderful scene of activity.
VENTNOR

TENNYSON'S HOUSE

The ride was all too short. It seemed but a moment until we
were stepping from the boat into the train at Ryde which was to
carry us the entire length of the island to Freshwater, twenty-three
miles away.
We arrived at Freshwater at sunset just as the bells were ringing
for vespers, and we walked with the country folk the half mile from
the station to the inn. Stopping long enough to leave our bags and
wraps, we continued across the meadows to Farringford, the
beautiful home of Tennyson. This was the realization of one of my
cherished desires.

The house possesses no architectural pretensions, but is


singularly attractive. It is a long, low, rambling structure absolutely
covered with creeping vines. I sat in Tennyson's chair, held his pen,
leaned on his desk and touched the books he loved. This was a
privilege because the public is not admitted since the young Lord
Tennyson has taken up his residence there.

Afterwards, I stood on the rustic bridge where Tennyson often


stood to watch the sea, seen far away through the trees. I sat in the
bower where he wrote "Enoch Arden," and strolled along the lanes
which wind over the three hundred acres comprising the estate.

It was with difficulty that I dragged myself away from this restful
spot, but I hope that I caught a bit of the inspiration that he found
there.

Another day from the top of a coach we saw the beautiful


country through which we had been whirled at dusk some days
before. We drove to the rocks at the "bottom of the island," called
the Needles; we wound through the cluster of cottages forming the
village of Freshwater—then on we went through a succession of
flowers on the hillside, flowers in the valleys, flowers by the sea, for
the Isle of Wight is composed of blossoms and all the variations of
green, with ever the blue sea as a background.

We had our tea in the garden of the little inn which nestles
under the wall of Carisbrooke Castle. After we had climbed to its
tower for the view and had returned to earth again, we continued on
to Newport and Ventnor.

If you ever arrive at that part of Ventnor called "Bonchurch,"


stay there. Whoever named it must have been color-blind.
SHANKLIN, ISLE OF WIGHT

STREET IN BONCHURCH

STOKE POGES:

A delightfullyrestful day has been spent at Stoke Poges, in that


peaceful old churchyard which inspired Gray's Elegy. The whole
place remains the same as in the poet's time—1717, except "Yon ivy-
mantled tower," which has been spoiled by a modern spire. But the
ivy refuses to "mantle" it, and with strange perverseness stops at
the tower, leaving the spire bare and "unloved" by the vine.

As you sit under the yew tree where Gray sat and dreamed, you
will realize the significance of his immortal lines:

"Full many a gem of purest ray serene


The dark unfathom'd caves of ocean bear:
Full many a flower is born to blush unseen,
And waste its sweetness on the desert air."

The scenery along the Thames Valley, from London to Slough, is


pleasing. On leaving the train at Slough, one finds all sorts of
carriages waiting to carry one to Stoke Poges, and on to Burnham
Beeches.

LAKESIDE, WINDERMERE, WEST VIEW VILLAS:

We left London, St. Pancras Station, via the Midland Railway,


stopping en route at Chesterfield long enough to see the
"Twisted Tower" of the cathedral. It was built in the fourteenth
century, and the book says, "A curious twist to the spire was caused
by the warping of the wood." The poor ignorant people say it was
the devil. It is very odd, whatever did it.
STOKE-POGES, WHERE GRAY'S "ELEGY"
WAS WRITTEN

We left the train at Leeds to see the ruins of Kirkstall Abbey,


catching the next through train by driving to Skipton, and here
began the most picturesque scenery I have found in England.

The valley of Craven consists of meadows similar to those of


Chester and Warwick, but they are softer and greener; the same
hedges, but darker, higher, and more velvety. The woods behind
them set them off to advantage, and here and there, sparkling in the
sunlight, are little lakes. The winding white roads and beautiful roses
are everywhere. We passed a cañon cut in the rocks, with cliffs as
high as one can see, then the blue hills of Cumberland burst on our
vision.
This mountain region, called the English Lake District, is said by
the English to be the most beautiful spot in the British Isles, but the
Scotch and the Irish each claim the same superlative. I shall see
them all, and shall give you an unprejudiced opinion, but certain it is
that within these limits lies a wealth of scenery not to be very far
surpassed anywhere.

Have you the slightest idea what an English meadow is like? I


had not, until today. This one has hills on either side with the clear
blue Windermere at their feet. The white roads wind in and out, with
this cluster of villas all covered with roses, and an old rustic bridge
near by. I am writing this in the sweetest and cleanest of rooms,
from the window of which I see the purple hills in the west and the
sun just sinking behind them.

EN ROUTE:

T he sail on Lake Windermere was delightful. The boat touched at a


number of picturesque places once frequented by Scott,
Wordsworth, Shelley and Southey, landing us at Ambleside about ten
in the morning. Here the coach was waiting to take us on one of the
loveliest drives in Great Britain. All the way we glided over the same
smooth roads, with mountains on one side and Lake Grasmere at
our feet. We visited the cottage where Wordsworth lived, the one in
which Coleridge died, and the home of Harriet Martineau. What
wonder that these dear people wrote so poetically! One must find
expression for one's dreams in this land of beauty.

We reached Keswick just in time to board the train for Penrith,


where we changed for Carlisle. Here we took time to visit the old
castle and the really fine cathedral before leaving for Melrose,
Scotland.
It is a mistaken idea that the English people sneer at or slight
Americans. Every well-informed Englishman acknowledges the
United States to be the most progressive nation on earth. Everything
American is sought after, and American ideas command the highest
price.

I have found the better class of English the most charming of


people, and their hospitality knows no limit. My stay here, away from
my native land, has been one bright dream of pleasure, made so
particularly by a dear old English couple, and by the family on the
house-boat.

And now, good-bye, bright, fragrant and flowery England!


SCOTLAND
I canna thole my ain toun, sin' I hae dwelt i' this;
To bide in Edinboro' reek wad be the tap o' bliss.
Yon bonnie plaid aboot me hap, the skirlin' pipes gae bring,
With thistles fair tie up my hair, while I of Scotia sing.
Kate Douglas Wiggin.

EDINBURGH:

M elrose Abbey by moonlight!


What a world of meaning those words hold for me! What a
wealth of history those ruins contain! Their story must be read
before coming, for the custodian's daughter, who was our guide, like
Stockton's Pomona, had learned her story by heart, and no amount
of questioning would bring forth any other facts save those in the
"book."

This morning Ruth and I hired wheels and rode to Abbotsford.


The beautiful home of Sir Walter Scott is after the style of many
castles we have seen, walled in with gardens, terraced lawns, parks
and drives. We plucked a bit of the ivy and holly hedge planted by
Sir Walter's own hand, and walked in the gardens he loved so well.
Imagine, if you can, a city of three hundred thousand
inhabitants, having in its heart an immense rock, with a castle on
top of it.

Edinburgh is rich in landmarks, in spite of the fact that it has


been burned to the ground twice since 1300. Its natural beauty
surpasses that of either London or Paris. It is built upon two ridges,
divided by a valley, which is now a park. The new town is situated to
the north of the park, and in this portion are found the modern
buildings and principal hotels. From my window I look out on the
marble features of Scott, whose monument is at the end of the park.

The picturesque "Old Town" begins with the castle on its huge
embankment and slopes down toward the south. It is here one finds
the historic landmarks crowding each other in dramatic interest.
Here, too, is brought vividly to mind the sad story of poor Queen
Mary.

In the valley between the old and new towns is found a wealth
of art and architecture not duplicated anywhere, for these Scots are
strong in their originality.

It was from the esplanade overlooking one of the perpendicular


sides of the castle rock, but which is now used as a drill-ground for
the soldiers in the barracks, that I had my first view of that man-
devised wonder, the Forth Bridge. I crossed it afterwards en route to
Glasgow.

A few days is but scant time to do justice to the landmarks of


Edinburgh, and it puzzles one to choose from among those orthodox
and those otherwise. St. Giles, the old Gray Friars and John Knox vie
with the haunts of Burns, Scott, Johnson and Boswell. The shops,
too, form no small part of the attractiveness of the street scene, and
the windows filled with articles done in plaids of the different clans
are alluring.
NATIONAL GALLERY, EDINBURGH,
CASTLE ON HILL IN BACKGROUND

GLASGOW:

T he chief difference, I find, between the English and Scottish castles


lies in the fact that the former are simply residences—walled to be
sure—while the latter are strongholds, generally perched on some
gigantic rock, and, incidentally, royalty resided in them long enough
to have their heads under the guillotine. Stirling Castle is no
exception to the rule, and it is therefore not visited by many women.

There is a long, hard climb up the hill leading to the


fortifications, for Stirling is still a garrisoned town, and the castle
stands on the edge of a steep, isolated rock overhanging the Forth.
Here are the steps where Mary, Queen of Scots, stood to survey her
possessions, the window out of which the body of Douglas was
thrown, and the raised dais, on the battlements, from which Queen
Victoria reviewed her troops. From the battlements there is a fine
view of the country for miles around, with the statue of Wallace to
be seen in the far distance. Just before crossing the drawbridge at
the entrance to the castle stands a bronze Robert Bruce, whose
features, even in iron, bring back the foremost of Scottish chiefs.

When a Scotchman tells you to do or see anything, he invariably


adds, "If the day be fine," and true enough much depends on the
"fineness" of the day in a country where it rains a little every day.
The good wishes had been so many and so fervent that we might
have a fine day for the coach drive through the Trossachs that
nature put on her brightest smile and never shed a tear until we
were under shelter.

The name Trossachs signifies "bristly country," and Scott, in his


"Lady of the Lake," tells how it "bristles" with beauty and romance.
That old story is, after all, the best guide to the lake region of
Scotland.

The big red coach, with its four white horses and red-coated
driver, meets the passengers as they alight from the traveling
carriages, and dashes away almost before they are seated. Then
follows in quick succession pictures of white roads bordered with
purple heather, with a background of the dark green of the
mountain; of a stone bridge spanning the blue waters of a salmon
stream; of a wild bit of mountain scenery, with a road seemingly
straight up its rugged sides; and last comes the view of the calm
waters of Loch Katrine.

The boat Rob Roy receives the party from the coach and rounds
Ellen's Isle, sailing almost the entire length of the beautiful loch.
When it finally lands, there is another coach waiting to carry us
across the mountains, and on to Inversnaid, where, after visiting the
waterfall, the train is taken for Glasgow.

DRYBURGH ABBEY, WHERE SIR WALTER


SCOTT IS BURIED

Glasgow is not a picturesque town—in fact, the Clyde is the


prettiest thing about it—but it is modern and progressive, and it has
two attractive public buildings, the cathedral and university.
AYR:

B urns's land lies between Glasgow and the sea, and from the
moment that one alights from the train, at each step is found
some haunt of the much-loved poet. It takes but a short time to
peep through the window into the room where Burns was born, and
to compare the humble cot where he lived his life with the
magnificent place he occupies in death. His tomb is set high up on a
hill in the midst of a park whose sides slope down to the bonnie
Doon.
IRELAND
When the glass is up to thirty,
Be sure the weather will be dirty.
When the glass is high, O very!
There'll be rain in Cork or Kerry.
When the glass is low, O Lork!
There'll be rain in Kerry and Cork.
* * * * * *
And when the glass has climbed its best,
The sky'll be weeping in the west.
Kate Douglas Wiggin.

T he shortest sea voyage between Scotland and Ireland is from


Stranraer to Larne. Stranraer is a short ride from Ayr, but the S. S.
Princess Victoria was five hours crossing the channel. It was cold
and rough, and many of the passengers were ill.

One of the most fascinating of trips is that to the Giant's


Causeway. From Larne the road takes its way through a number of
thriving towns, and the country looks neat and has an air of the
well-to-do.
At Portrush the scene changes, and becomes, almost at once,
one of wild ruggedness. The cliffs rise high on one side, and the
steep precipice at the edge of the tramway goes down to the sea on
the other. This is an extraordinary coast. The action of the waves
and the tides on the limestone has made the rocks take on fantastic
shapes. The ocean is always tempestuous. It must be beautiful from
the water, but nothing save small boats can venture here, so the
view is almost unknown. This sort of scene continues until we reach
Dunluce Castle.

Perched on the summit of an isolated rock, not far from the


shore, is this picturesque fortress, separated from the mainland by a
deep chasm. The castle is reached by a drawbridge, while beneath,
the waves beat madly against the sides of the rock, black with the
age of centuries.

The word "causeway" means paving, and these Irish giants


paved well. Basaltic rock is plentiful along the north coast, but this
particular district alone embraces these odd varieties of form. The
caves along the coast can be seen only by means of rowboats.
These are manned by strong and trustworthy sailors. The sea is very
rough, and the boatmen delight in making the trip seem even more
hazardous than perhaps it really is. After the caves have been
explored the boat is rowed to the extreme end of the Causeway, and
it is during the walk back that we get the best idea of these
wonderful formations, and have a hair-raising experience on a
narrow path three hundred and twenty feet in air. At first it was
delightful—high, of course, but with a broad path. On turning a
sharp corner, suddenly we came to a narrowing of the way, with
nothing but rocks and sky above, and rocks and sea below. We
dared not turn back, and we walked that terrible pass until we came
to a widening in the path—it seemed hours—and then Ruth and I sat
down and cried from sheer exhaustion. It cost us ten shillings to
enter by the sea and six to make our exit by land.
How is that for the downtrodden Irish?

KILLARNEY:

I wish I were a poet! But even the poet laureate, who recently
visited here, says, "Words cannot do justice to this sweet, sad
scene." His word "sad" pleased me, for I said yesterday to Ruth that
the scenery of Ireland has a tenderness about it that makes one be
quiet and think things.

DUNLUCE CASTLE
We started at nine-thirty in a four-horse coach with a bugler. The
road lies along the north side of the lower lake, and it wasn't long
before the exquisite mountain scenery came into view. The Purple
Mountains grew more interesting at every step. Presently we came
to Kate Kearney's cottage, and our Irish guide turned and asked, in
the richest of brogues:

"Oh! have you ever heard of Kate Kearney?


She lived at the Lakes of Killarney;
One glance of her eye would make a man die;
And have you never heard of Kate Kearney?"

Further on we struck the mountain pass, where the coach could


not go. We dismounted and were placed on ponies. I thought at first
I could not ride one, but I soon got used to the saddle, and I would
not have missed the wild, weird pass over the mountain for
anything. There was nothing "sad" or "tender" about that. It was
fearful, awesome and mysterious.

We left the ponies at the foot of the mountains and paid toll into
Lord Brandon's estate in order to reach the boats. Lunch was served
on the banks of the upper lake.

These lakes have to be explored in rowboats, on account of the


narrows, a pass between the rocks not more than ten feet apart.
Such varied beauty I have seen nowhere else. The tender grace of
the heather-strewn valley against the background of hills, the
frequent change from the gentle to the stern, the calm-flowing
waters, the smiling cascades turning into dashing cataracts over
dangerous piles, are a never-ending source of surprises.

The upper lake is more placid and less changeable, but the lower
has every change, from smooth, glass-like waters to the rapids,
which we "shoot" in no fearless manner. Finally we alight on
Innisfallen Island to see the ruins of the abbey; then we cross to
Ross Castle. Here another coach and four was in waiting to carry us
home. After ten miles by coach, five on horseback and thirteen by
boat, I actually dress for dinner.

We were up with the larks this morning, packed everything very


carefully, sent the basket off by carted luggage, and nearly came to
blows with the stupid paddy at the station over the settlement.

After breakfast the coach came dashing up, and away we flew
again, over the purple hills, through shady lanes, past the wee farms
and the hovels, catching glimpses of castles, churches and ruins.
The most beautiful of all is Muckross Abbey. I had no idea we could
possibly repeat the pleasures of yesterday, but in some respects we
exceeded them. Our road today wound up and around Eagle Nest
Mountain, in the dark recesses of which the eagle builds its nest.
Here, too, is the home of the famous Killarney echo. The effect
produced by the notes of a bugle is almost supernatural.

The coachmen have a clever manner of talking to the echoes.


For instance, ours called out, "Pat, were you drunk last night?" and
the confession came back from a thousand hills, "Drunk last night,
drunk last night, drunk last night."

The literary Killarnian claims for this beautiful region that it was
the ruins of the old castle on the shores of the Middle Lake which
called forth Tennyson's masterpiece, "The Bugle Song."

The Purple Mountains take their name from the purple of the
heather. One can see every shade, from the light pink-lavender to
the dark, almost red, purple.
We arrived at Glengariff just as the sun was sinking. The valley,
the lakes, the mountains, the red coach, with its four big horses
darting in and out of the winding road, and finally galloping up to
the exquisite little inn at Glengariff, high on a knoll overlooking the
blue waters of the Bay of Bantry, are among the delightful details of
today's picture.

The shore line of this attractive bay can be appreciated only


when one is taken in a small boat, threading one's way through the
numberless private yachts that dot its waters. One of the gentlemen
of our party, thinking to have some sport with the boatman, said
that only one lady could go in each boat, and that he must choose
the one he wished to go with him. After a critical survey the answer
came, "Divil a step will I go without the both of yez!" and he handed
us both into the boat, and left the gentlemen to seek a boat by
themselves.

CORK:

W e left the coach at Bantry and took an observation car to Cork.


After a rest of a few hours and a dainty luncheon a jaunting-car
"shook" us over the road to Blarney Castle. The road lies through a
beautifully cultivated country. There is a charm about the sweet old
castle that is indescribable. The view from the top is superb, taking
in the valley of the Lee, with the old Roman bridge in the far
distance.

When any one tells you that he kissed the Blarney stone, take it
with several grains of salt. It is a physical impossibility for one who
wears petticoats.

Cork is, to my mind, the prettiest town in all Ireland. It lies in


the midst of limestone quarries, and is white to a degree. I had not
read Thackeray's "Sketch Book" before I came here, and I wondered
why some one had not raved over this magnificent part of the world.
I have since been delighted to find that he did rave—I use the word
advisedly—as no one but Thackeray can.

Cork has more well-known landmarks than any other place in


Ireland. In a little three-storied bell-tower in the center of the town
hangs the chime of bells made famous by Francis Mahony in his—

"With deep affection and recollection


I often think of the Shandon bells."

One of the pleasant drives from Cork takes one to Sir Walter
Raleigh's home at Youghal. For more than four hundred years it has
stood with but little change. Attached to the grounds is the garden
where Raleigh experimented with the potato, which here was first
grown in Ireland.

We were a rather solemn lot on the drive to Queenstown, for all


but Ruth and me were to sail from there for home. This seeing
people off isn't what "it's cracked up" to be, especially when they are
off for the land where "some one loves you and thinks of you far
away," but we wished them bon voyage, and Ruth and I turned our
hard-set faces northward.

DUBLIN—Great Denmark Street:

"No wind can drive my bark astray,


Nor change the tide of destiny."

And so this all too happy summer must come to a close. I remain
here to study, and Ruth goes to Iceland. We shall meet in the spring,
when I shall have taken my degree (?), and go to sunny Italy
together.

It is said that to travel through Europe with one and still remain
friends, stamps both as remarkably amiable persons. Without
wishing to seem egotistical, I'd like you to know that before bidding
Ruth good-bye she invited me to join her later in this jaunt through
Italy.

I was sitting on the deck of the ship that was to carry Ruth away
from me, looking at the lights out over Dublin Bay, when some one
touched me on the shoulder, and, on turning around, there stood
dear Miss B., who was with us for a time at Killarney. I met her
father on the street the other day, and told him of Ruth's intended
departure. They were very good to come to us that night, and I shall
never forget their kindness in helping me over these first days
without my blessed Ruth. Through them I have made some
charming friends who occupy the time before I start in to study.

I have had a delightful outing, one which enabled me to see,


and in an uncommon manner, certain out-of-the-way places where
the casual tourists rarely go, and it has all been due to the friends of
Miss B. These Irish know how to do things well.

We started away, a regular cavalcade, with most of the women


in the coach and a few on horseback. The servants went ahead with
the wagons carrying the viands and rugs, and, oh, a hundred things
we Americans would never think of.

Dublin has more pleasure resorts at her door than any other city
in the world. We drove out through Phœnix Park, passing the
summer home of the Lord Lieutenant of Ireland. We made our first
stop at Killiney Castle to get the fine view of Dublin Bay. It was from
this spot that the poet wrote:

"O Bay of Dublin!


My heart you're troublin'.
Your beauty haunts me
Like a fever dream."

Then we dashed away to Bray and Bray's Head, along the


Esplanade, through the Scalp, a wild bit of country in the county of
Wicklow, and the Dargle, which is a romantic glen. We never go
slowly—the horses are either galloped, or stopped altogether. Then
on we flew through Enniskerry, a lovely little village, where
everybody stopped or ran to the door to watch us go by, with a
wave of the hand, and always a "God bless ye!"

I could not believe such magnificence was possible in Ireland as


was found at Powers Court had I not seen it with my own eyes. It is
the finest private mansion I have seen in all my travels. The Vale of
Avoca, which called from Moore these lines,

"There is not in the wide world a valley so sweet


As the vale on whose bosom these bright waters meet,"

did not appeal to me so much as did Killarney.

The city of Kilkenny, called the "Marble City," impressed itself on


me. The streets are paved with marble of their own quarrying, and
what is better, the inhabitants have fire without smoke, from a
peculiar coal found in that district. They also claim to have water
without mud, and earth without bog, and however true these boasts
may be, it is a wonderfully clean city. The coach was sent back from
this place by the servants, and we returned by train.
It all seems very tame in this telling of mine, but the trip, every
moment of it, was delightful. Sometimes we would all get out and
walk; sometimes the ladies would exchange with the men and ride
horseback; or when it would rain for a few moments the men would
crowd into the coach. Then there would be good fun, and I could
get an idea of their thoughts. They are great story-tellers, these
Irish, and have such warm hearts. And the songs they sang, when
shall I ever hear such again? And yet there was not a young person,
that is, one under thirty, in the party.

Other things besides wine, my dear, "improve with age."

There is a pathos about the love of an Irishman for his country


that is most touching, and each county vies with the others in
patriotic loyalty; and let me whisper in your ear, that the Irish gentry
are far and away ahead of "what the world thinks" they are. In fact,
they are "deloitful."

I suppose you have noticed the number of "Kills" which form


some part of many of the names I have referred to. "Kil" is the
Gaelic for "church."

One of my Irish friends told the story of an Englishman who


went over to Ireland and fell upon the following conversation
between two tough-looking natives:

"I'm afther being over to Kilpatrick," said the first.

"An' I," replied the other, "am afther being over to Kilmary."

"And where are you going now?" asked number one.

"To Kilmore," was the answer.

The frightened Englishman concluded not to tarry in such a


bloodthirsty country, and stood not upon the order of his going.
Since writing that last letter I have been very busy getting in
trim for work, and at last I'm "fit."

I have been taking my afternoons to see this wonderful city. I


told you, did I not, that because I am in these blessed petticoats, I
am obliged to recite "apart"—not apart from the petticoats, but apart
from the unpetticoated sort.

My home is in quite a good-looking house, and it is well


furnished, but the landlady is away, and the maidens do it up when
and how they please. I have a large room "front," and as I study
here every morning, and write much of the remainder of the time,
my room is "tidied" only when I ask for it, and then, of course, it is
an extra.

Will you believe me when I tell you that nowhere in Europe have
I seen more lovely or better dressed women than right here on
Sackville Street? I have accounted for it, in some degree, by the fact
that our Irish cousins follow the American styles more closely than
do any of their immediate sisters. The Irish woman is always in good
form. One never sees her wearing any sort of jewelry before
luncheon. She is usually found in the morning in a short, tailored
skirt, a chic blouse and hat; some dainty confection of lace and
muslin in the afternoon; and, almost without exception, the middle
class, as well as the gentry, "dress" for dinner; then it is one sees
the beautiful jewels handed down by their forebears.

The college buildings are delightfully quaint, with multitudes of


old-fashioned wee window-panes which stud their faces. Statues of
two of Ireland's beloved sons, Burke and Goldsmith, are on either
side of the entrance. Opposite is the famous Bank of Ireland,
beautiful in design, and the general post-office. Statues of
"Hibernia," "Mercury" and "Fidelity" adorn the latter.
For some reason an Irishman, in his native country, will not
admit ignorance on any subject. He would rather tell you wrongly
than to say, "I don't know."

TRINITY COLLEGE, DUBLIN

Some one asked a "jarvey" what those statues I have just


mentioned were. Pat hadn't their names handy in his mind, so he
drew on his imagination, and replied: "Thim's the twelve apostles,
sur."

"Twelve apostles," shouted the inquirer; "why, man, there are


only three of them!" To which Pat, not to be caught by such a trifle,
said:
"Sure, an' yer honor wouldn't have thim all out in this dom rain,
would ye? The rest of 'em are inside sortin' o' the letters."

The first day I was shown over Dublin my guide, in pointing out
the college, said: "This is the Library, and an institute for learning." I
asked, "How far does the Library extend?" meaning, which was the
Library and which the Institute. The honest, but thick-headed, paddy
replied, "To the roof, mum."

The comparative neglect by tourists of a country like Ireland,


where nature has lavished her charms with such wonderful
profusion, can only be explained by its hitherto unsettled condition,
and its long-a-dying notoriety for inferior accommodations and
modes of transportation. But whatever difficulties and discomforts
may have existed to deter the traveler in former days, it seems to
me that little now is wanting to render a tour through Ireland all that
the rational traveler can desire.

It is well nigh impossible to tell of the exquisite scenery of the


beautiful island without seeming fulsome. Almost every county so
teems with prehistoric remains, and the island is so begirt with
varied attractiveness, that it is as alluring to the student and artist as
it is to the pleasure seeker.
ITALY
For Italy, my Italy, mere words are faint!
No writer's pencil can convey thy heaven's blue,
Thy languorous bay.
Thou art thine own interpreter.
I dream and wake and find no words for her—
For Italy's soft-storied charms
I throw the English words away.
Her gondolas drip through the night—
I stretch my arms toward Napoli,
And "Monte Bella" softly say.
Harriet Axtell Johnstone.

SORRENTO:

H ow splendid it seems to be free again! And yet I do believe it does


one good—having been out of the habit of studying—to take a
few months every year or so and to give close application to some
subject.

I was glad when the time came to end traveling and to begin
study; and now I am glad that I can cease my studies and again
begin sight-seeing.
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like