The material presented in this section builds on an understanding of the following information:
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. Here are some examples used in a class definition:
classdef MyClass (Sealed = true) % Logical value sets attribute properties (Constant = true) CnstProp = 2^.5; end properties Prop1 = MyClass.setupAccount; % Static method of this class Prop2 = MyConstants.Minimum; % Constant property from another class Prop3 = MyConstants.Rate*MyClass.CnstProp % Constant property from this class Prop4 = AccountManager; % A class constructor end end
MATLAB does not call property set methods when assigning the result of default value expressions to properties. (See Property Access Methods for information about these special methods.)
Class definitions specify attribute values using an expression
that assigns the desired value to the named attribute. For example,
this assignment makes
MyClass sealed (cannot be
classdef MyClass (Sealed = true)
It is possible to use a MATLAB expression on the right
side of the equals sign (
=) as long as it evaluates
this expression cannot use any definitions from
the class definition file, including any constant properties, static
methods, and local functions.
While it is possible to use conditional expressions to specify attribute values, doing so can cause the class definition to change based on external conditions.
See Attribute Specification for more information on attribute syntax.
Property definitions allow you to specify default values for
properties using any expression that has no reference to variables.
Myclass defines a constant property
Deg2Rad) and uses it in an expression that defines
the default value of another property (
The default value expression also uses a static method (
defined by the class:
classdef MyClass properties (Constant) Deg2Rad = pi/180; end properties PropA = sin(Deg2Rad*MyClass.getAngle([1 0],[0 1])); end ... methods (Static) function r = getAngle(vx,vy) ... end end end
Expression in class methods execute like expressions in any function — MATLAB evaluates an expression within the function's workspace only 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.
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.
MATLAB evaluates the expressions in class definitions only when the class is initialized. Initialization occurs before the class is first used.
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 reevaluation.
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)
The following example shows how value and handle object behave
when assigned to properties as default values. Suppose you have the
ContClass defines the object
that is created as a default property value, and
a property that contains a
classdef ContClass properties TimeProp = datestr(now); % Assign current date and time end end classdef ClassExp properties ObjProp = ContClass; end end
MATLAB creates an instance of the
ClassExp class is first used. MATLAB initializes
both classes at this time. All instances of
a copy of this same instance of
a = ClassExp; a.ObjProp.TimeProp ans = 08-Oct-2003 17:16:08
TimeProp property of the
contains the date and time when MATLAB initialized the class.
Creating additional instances of the
shows that the date string has not changed:
b = ClassExp; b.ObjProp.TimeProp ans = 08-Oct-2003 17:16:08
Because this example uses a value class for the contained object,
each instance of the
ClassExp has its own copy
of the object. For example, suppose you change the value of the
on the object contained by
b.ObjProp.TimeProp = datestr(now) ans = 08-Oct-2003 17:22:49
The copy of the object contained by object
a.ObjProp.TimeProp ans = 08-Oct-2003 17:16:08
Now consider the difference in behavior if the contained object is a handle object:
classdef ContClass < handle properties TimeProp = datestr(now); end end
Creating two instances of the
shows that MATLAB created an object when it initialized the
used a copy of the object handle for each instance
ClassExp class. This means there is one
ObjProp property of each
contains a copy of its handle.
Create an instance of the
and note the time of creation:
a = ClassExp; a.ObjProp.TimeProp ans = 08-Oct-2003 17:46:01
Create a second instance of the
ObjProp contains the handle of the same object:
b = ClassExp; b.ObjProp.TimeProp ans = 08-Oct-2003 17:46:01
Reassign the value of the contained object's
b.ObjProp.TimeProp = datestr(now); b.ObjProp.TimeProp ans = 08-Oct-2003 17:47:34
ObjProp property of object
a handle to the same object as the
a, the value of the
has changed on this object as well:
a.ObjProp.TimeProp ans = 08-Oct-2003 17:47:34