Object Behavior
In this section...
“Two Copy Behaviors” on page 16-2
“Handle Object Copy” on page 16-2
“Value Object Copy Behavior” on page 16-2
“Handle Object Copy Behavior” on page 16-3
“Testing for Handle or Value Class” on page 16-5
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
16 Using Objects
16-2
In this section...
“Two Copy Behaviors” on page 16-2
“Handle Object Copy” on page 16-2
“Value Object Copy Behavior” on page 16-2
“Handle Object Copy Behavior” on page 16-3
“Testing for Handle or Value Class” on page 16-5
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
16 Using Objects
16-2
