Main Content

Copying Objects

Two Copy Behaviors

There are two fundamental kinds of MATLAB® objects — handles and values.

Value objects behave like MATLAB fundamental types with respect to copy operations. Copies are independent values. Operations that you perform on one object do not affect copies of that object.

Handle objects are referenced by their handle variable. Copies of the handle variable refer to the same object. Operations that you perform on a handle object are visible from all handle variables that reference that object.

Handle Object Copy

If you are defining classes and want to support handle object copy, see Implement Copy for Handle Classes.

Value Object Copy Behavior

MATLAB numeric variables are 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. b is unchanged:

a = 6;
b
b =
     8

Clearing a does not affect b:

clear a
b
b =
     8

Value Object Properties

The copy behavior of values stored as properties in value objects is the same as numeric variables. For example, suppose vobj1 is a value object with property a:

vobj1.a = 8; 

If you copy vobj1 to vobj2, and then change the value of vobj1 property a, the value of the copied object's property, vobj2.a, is unaffected:

vobj2 =vobj1;
vobj1.a = 5;
vobj2.a
ans =
     8

Handle Object Copy Behavior

Here is a handle class called HdClass that defines a property called Data.

classdef HdClass < handle
   properties
      Data
   end
   methods
      function obj = HdClass(val)
         if nargin > 0
            obj.Data = val;
         end
      end
   end
end

Create an object of this class:

hobj1 = HdClass(8)

Because this statement is not terminated with a semicolon, MATLAB displays information about the object:

hobj1 = 

  HdClass with properties:

    Data: 8

The variable hobj1 is a handle that references the object created. Copying hobj1 to hobj2 results in another handle referring to the same object:

hobj2 = hobj1
hobj2 = 

  HdClass with properties:

    Data: 8

Because handles reference the object, copying a handle copies the handle to a new variable name, but the handle still refers to the same object. For example, given that hobj1 is a handle object with property Data:

hobj1.Data
ans =

     8

Change the value of hobj1's Data property and 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 =

    17

Reassigning Handle Variables

Reassigning a handle variable produces the same result as reassigning any MATLAB variable. When you create an 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).

Clearing Handle Variables

When you clear a handle from the workspace, MATLAB removes the variable, but does not remove the object referenced by the other handle. However, if there are no references to an object, MATLAB destroys the object.

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 with properties:

    Data: 256

If you clear both hobj1 and hobj2, then there are no references to the object. MATLAB destroys the object and frees the memory used by that object.

Deleting Handle Objects

To remove an object referenced by any number of handles, use delete. Given hobj1 and hobj2, which both refer to the same object, delete either handle. MATLAB deletes the object:

hobj1 = HdClass(8);
hobj2 = hobj1;
delete(hobj1)
hobj2
hobj2 = 

  handle to deleted HdClass

Use clear to remove the variable from the workspace.

Modifying Objects

When you pass an object to a function, MATLAB passes a copy of the object into the function workspace. If the function modifies the object, MATLAB modifies only the copy of the object that is in the function workspace. The differences in copy behavior between handle and value classes are important in such cases:

  • Value object — The function must return the modified copy of the object. To modify the object in the caller’s workspace, assign the function output to a variable of the same name

  • Handle object — The copy in the function workspace refers to the same object. Therefore, the function does not have to return the modified copy.

Testing for Handle or Value Class

To determine if an object is a handle object, use the isa function. If obj is an object of some class, this statement determines if obj is a handle:

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 =

     1

hobj is also a containers.Map object:

isa(hobj,'containers.Map')
ans =

     1

Querying the class of hobj shows that it is a containers.Map object:

class(hobj)
ans =

containers.Map

The class function returns the specific class of an object.

Related Topics