MATLAB Object-Oriented Programming

(Joyce) #1

Handle Object Behavior


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

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.

Handle Object Behavior
Free download pdf