MATLAB Object-Oriented Programming

(Joyce) #1

Evaluation of Expressions in Class Definitions


In this section...
“Why Use Expressions” on page 6-10
“Where to Use Expressions in Class Definitions” on page 6-10
“How MATLAB Evaluates Expressions” on page 6-13
“When MATLAB Evaluates Expressions” on page 6-13
“Expression Evaluation in Handle and Value Classes” on page 6-14

Why Use Expressions


An expression used in a class definition can be any valid MATLAB statement that
evaluates to a single array. Use expressions to define property default values and in
attribute specifications. Expressions are useful to derive values in terms of other values.
For example, suppose you want to define a constant property with the full precision value
of 2 π. You can assign the property the value returned by the expression 2*pi. MATLAB
evaluates the function the when first loading the class.

For information on assign property default values and attribute values, see the following
topics:


  • “Property Definition” on page 8-17

  • “Attribute Specification” on page 5-22


Where to Use Expressions in Class Definitions


Here are some examples of expressions used in a class definition:

classdef MyClass
% Some attributes are set to logical values
properties (Constant = true)
CnstProp = 2*pi
end
properties
% Static method of this class
Prop1 = MyClass.setupAccount
% Constant property from this class
Prop2 = MyClass.CnstProp

6 Defining and Organizing Classes

Free download pdf