Copying Objects

Two Copy Behaviors

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 behave 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 are visible from all handles that reference that object.

Value Object Copy Behavior

MATLAB numeric variables are 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 =
     8

Value Object Properties

The 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

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 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: 8

  Methods, Events, Superclasses

The 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: 8

 Methods, Events, Superclasses

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

     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

Copy Method for Handle Classes

Handle classes can derive copy functionality from the matlab.mixin.Copyable class. Class designers can investigate the use of this class in their class design.

Reassigning Handle Variables

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

Clearing Handle Variables

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: 256

 Methods, Events, Superclasses

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

Deleting Handle Objects

To remove an object referenced by any number of handles, use delete. 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

  Methods, Events, Superclasses

See Destroying Objects for more information about object lifecycle.

Modifying Objects

When you pass an object to a function, MATLAB follows pass by value semantics. This means that MATLAB passes a copy of the object to the function. If you modify the object in the function, MATLAB modifies only the copy of the object. The differences in copy behavior between handle and value classes are important in such cases:

  • Value class — The function must return the modified copy of the object to the caller.

  • Handle class — The copy refers to the same data as the original object. Therefore, the function does not need to return the modified copy.

See Passing Objects to Functions for more information.

More Information About Handle and Value Classes

For information about handle and value classes for class designers, see Handle Classes in the Object-Oriented Programming documentation.

Testing for Handle or Value Class

If you are writing MATLAB programs that copy objects, you might need to determine if any given object is a handle or a 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 =

     1

hobj is also a containers.Map object:

isa(hobj,'containers.Map')

ans =

     1

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

Was this topic helpful?