| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
There are two fundamental kinds of MATLAB classes—handles and values.
Value classes create objects that behave like ordinary MATLAB variables with respect to copy operations. Copies are independent values. Operations that you perform on one object do not affect copies of that object.
Handle classes create objects that are sometimes referred to as references. This is because a handle, and all copies of this handle, refer to the same underlying object. When you create a handle object, you can copy the handle, but not the data referenced by the object's properties. Any operations you perform on a handle object affects all copies of that object. Handle Graphics objects behave in this way.
For more detailed information about handle and value classes, see Value or Handle Class — Which to Use in the Object-Oriented Programming documentation.
MATLAB numeric variables exhibit the behavior of value objects. For example, when you copy a to the variable b, both variables are independent of each other. Changing the value of a does not change the value of b:
a = 8; b = a;
Now reassign a and b is unchanged:
a = 6;
b
b =
8
Clearing a does not affect b:
clear a
b
b =
8The copy behavior of values stored as properties in value objects is the same. For example, suppose vobj1 is a value object with property a:
vobj1.a = 8; % Property is set to a value
If you copy vobj1 to vobj2, and then change the value of vobj1 property a, you can see that the value of the copied object's property vobj2.a is unaffected:
vobj2 =vobj1;
vobj1.a = 5;
vobj2.a
ans =
8
Suppose you have a handle class called HdClass that defines a property called Data, and that you create an object of this class with the following statement:
hobj1 = HdClass(8)
Because this statement is not terminated with a semicolon, MATLAB displays information about the object:
hobj1 =
HdClass handle
Properties:
Data: 8The variable hobj1 is a handle that references the object created. Copying hobj1 to hobj2 results in another handle (the variable hobj2) referring to the same object:
hobj2 = hobj1
hobj2 =
HdClass handle
Properties:
Data: 8Because handle objects reference the data contained in their properties, copying an object copies the handle to a new variable name, but the properties still refer to the same data. For example, given that hobj1 is a handle object with property Data:
hobj1.Data
ans =
8When you change the value of hobj1's Data property, the value of the copied object's Data property also changes:
hobj1.Data = 5;
hobj2.Data
ans =
5
Because hobj2 and hobj1 are handles to the same object, changing the copy, hobj2, also changes the data you access through handle hobj1:
hobj2.Data = 17;
hobj1.Data
ans =
17Reassigning a handle variable produces the same result as reassigning any MATLAB variable. When you create a new object and assign it to hobj1:
hobj1 = HdClass(3.14);
hobj1 references the new object, not the same object referenced previously (and still referenced by hobj2).
When you clear a handle from the workspace, MATLAB removes the variable, but does not removed the object referenced by the handle. Therefore, given hobj1 and hobj2, which both reference the same object, you can clear either handle without affecting the object:
hobj1.Data = 2^8;
clear hobj1
hobj2
hobj2 =
HdClass handle
Properties:
Data: 256If you clear both hobj1 and hobj2, then there are no references to the object and MATLAB deletes the object and frees the memory used by that object.
To remove an object referenced by any number of handles, you delete the object. Given hobj1 and hobj2, which both reference the same object, if you delete either handle, MATLAB deletes the object:
hobj1 = HdClass(8); hobj2 = hobj1; delete(hobj1) hobj2 hobj2 = deleted HdClass handle
See Destroying Objects for more information about object lifecycle.
If you are writing MATLAB programs that copy objects, you might need to determine if any given object is a handle or value. To determine if an object is a handle object, use the isa function:
isa(obj,'handle')For example, the containers.Map class creates a handle object:
hobj = containers.Map({'Red Sox','Yankees'}, {'Boston','New York'});
isa(hobj,'handle')
ans =
1hobj is also a containers.Map object:
isa(hobj,'containers.Map')
ans =
1If you query the class of hobj, you see that it is a containers.Map object:
class(hobj) ans = containers.Map
The class function returns the specific class of an object, whereas isa returns true for any of the object's superclasses as well. This behavior is consistent with the object-oriented concept that an object is a member of all its superclasses. Therefore, it is true that a containers.Map object is a handle object and a containers.Map object.
There is no equivalent test for value classes because there is no value base class. If an object is a value object, isa(object,'handle') returns false (i.e., logical 0).
See Map Containers for more information on the containers.Map class.
![]() | Getting Information About Objects | Destroying Objects | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |