MATLAB Object-Oriented Programming

(Joyce) #1

Implement Copy for Handle Classes


In this section...
“Copy Method for Handle Classes” on page 7-35
“Customize Copy Operation” on page 7-36
“Copy Properties That Contain Handles” on page 7-37
“Exclude Properties from Copy” on page 7-40

Copy Method for Handle Classes


Copying a handle variable results in another handle variable that refers to the same
object. You can add copy functionality to your handle class by subclassing
matlab.mixin.Copyable. The inherited copy method enables you to make shallow
copies of objects of the class. The CopyObj class shows the behavior of copy operations.

classdef CopyObj < matlab.mixin.Copyable
properties
Prop
end
end

Create an object of the CopyObj class and assign the handle of a line object to the
property Prop.

a = CopyObj;
a.Prop = line;

Copy the object.

b = copy(a);

Confirm that the handle variables a and b refer to different objects.

a == b

ans =

logical

0

Implement Copy for Handle Classes
Free download pdf