MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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')

16 Using Objects

Free download pdf