MATLAB Object-Oriented Programming

(Joyce) #1

Attempting to create an instance causes an error:


a = VectorAngle([1,0],[0,1])


Error using VectorAngle
Unable to update the class 'VectorAngle' because the new definition contains an
error:
Undefined function or variable 'vx'.


Expressions in Class Methods


Expression in class methods execute like expressions in any function. MATLAB evaluates
an expression within the function workspace when the method executes. Therefore,
expressions used in class methods are not considered part of the class definition and are
not discussed in this section.


How MATLAB Evaluates Expressions


MATLAB evaluates the expressions used in the class definition without any workspace.
Therefore, these expressions cannot reference variables of any kind.


MATLAB evaluates expressions in the context of the class file, so these expressions can
access any functions, static methods, and constant properties of other classes that are on
your path at the time MATLAB initializes the class. Expressions defining property default
values can access constant properties defined in their own class.


When MATLAB Evaluates Expressions


MATLAB evaluates the expressions in class definitions only when initializing the class.
Initialization occurs before the first use of the class.


After initialization, the values returned by these expressions are part of the class
definition and are constant for all instances of the class. Each instance of the class uses
the results of the initial evaluation of the expressions without re-evaluation.


If you clear a class, then MATLAB reinitializes the class by reevaluating the expressions
that are part of the class definition. (see “Automatic Updates for Modified Classes” on
page 5-51)


Evaluation of Expressions in Class Definitions
Free download pdf