MATLAB Object-Oriented Programming

(Joyce) #1
MATLAB calls the delete method on the object, the delete methods for any objects
contained in properties, and the delete methods for any initialized base classes.

Depending on when the error occurs, MATLAB can call the class destructor before the
object is fully constructed. Therefore class delete methods must be able to operate on
partially constructed objects that might not have values for all properties. For more
information, see “Support Destruction of Partially Constructed Objects” on page 7-18.

For information on how objects are destroyed, see “Handle Class Destructor” on page 7-
16.

Output Object Suppressed


You can suppress the assignment of the class instance to the ans variable when no output
variable is assigned in a call to the constructor. This technique is useful for apps that
creates graphical interface windows that hold onto the constructed objects. These apps
do not need to return the object.

Use nargout to determine if the constructor has been called with an output argument.
For example, the class constructor for the MyApp class clears the object variable, obj, if
called with no output assigned:

classdef MyApp
methods
function obj = MyApp
...
if nargout == 0
clear obj
end
end
...
end
end

When a class constructor does not return an object, MATLAB does not trigger the
meta.class InstanceCreated event.

9 Methods — Defining Class Operations

Free download pdf