This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

handle class

Superclass of all handle classes


classdef MyHandleClass < handle

To define a handle class, derive your class from handle using the syntax shown in the preceding classdef code.

The handle class is the superclass for all classes that follow handle semantics. A handle is a reference to an object. If you copy an object's handle variable, MATLAB® copies only the handle. Both the original and copies refer to the same object. For example, if a function modifies a handle object passed as an input argument, the modification affects the original input object.

In contrast, nonhandle objects (that is, value objects) are not references. Functions must return modified value objects to change the object outside of the function's workspace.

See Object Modification for information on passing objects to functions.

If you want to create a class that defines events, you must derive that class from the handle class.

The handle class is an abstract class, so you cannot create an instance of this class directly. You use the handle class to derive other classes, which can be concrete classes whose instances are handle objects. See Handle Classes for information on using handle classes.


addlistenerCreate event listener bound to event source
deleteDelete handle object
findobjFind handle objects
findpropFind object
isvalidDetermine valid handles
listenerCreate event listener without binding to event source
notifyNotify listeners that event is occurring
relationaloperatorsDetermine equality or sort handle objects


ObjectBeingDestroyedTriggered when the handle object is about to be destroyed, but before calling the delete method. Listener callbacks execute before MATLAB destroys the handle object.

Destroying Handle Objects

MATLAB destroys handle objects when there are no references to the object. You can explicitly remove a handle object by calling its delete method. The handle class enables you to control what happens when handle objects are destroyed, either implicitly when no references exist or explicitly when you delete the object.

Define delete Method in Subclass

Subclasses of handle can define their own delete method. MATLAB calls this method when deleting an object of the subclass, enabling your subclass to perform any specific actions necessary before deleting a subclass object.

For more information, see Handle Class Destructor.

Create Listener for ObjectBeingDestroyed Event

Any code can respond to the pending deletion of a handle object by defining a listener for that object’s ObjectBeingDestroyed event. MATLAB triggers this event before calling the object’s delete method.

For more information on using events and listeners, see Events and Listeners Syntax.



For information on class attributes, see Class Attributes.


Derive Class from handle

Derive MyClass from handle:

classdef MyClass < handle
      function obj = MyClass(arg)
         if nargin > 0
            obj.Prop1 = arg;

Introduced in R2008a

Was this topic helpful?