MATLAB Object-Oriented Programming

(Joyce) #1
If you delete the object by deleting any one of the existing handles, all copies are now
invalid because you deleted the single object to which all handles refer.

Deleting a handle object is not the same as clearing the handle variable. In the graphics
object hierarchy, the parent of the object holds a reference to the object. For example, the
parent axes hold a reference to the line object referred to by l1 and l2. If you clear both
variables from the workspace, the object still exists.

For more information on the behavior of handle objects, see “Handle Object Behavior” on
page 1-9.

User-Defined Value Classes


MATLAB associates objects of value classes with the variables to which you assign the
object. When you copy a value object to another variable or pass a value object to a
function, MATLAB creates an independent copy of the object and all the data contained by
the object. The new object is independent of changes to the original object. Value objects
behave like MATLAB numeric and struct classes. Each property behaves essentially like
a MATLAB array.

Value objects are always associated with one workspace or temporary variable. Value
objects go out of scope when their variable goes out of scope or is cleared. There are no
references to value objects, only copies that are independent objects.

Value Object Behavior

Here is a value class that stores a value in its Number property. The default property
value is the number 1.

classdef NumValue
properties
Number = 1
end
end

Create a NumValue object assigned to the variable a.

a = NumValue

a =

NumValue with properties:

7 Value or Handle Class — Which to Use

Free download pdf