|On this page…|
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 subclassed).
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 to logical true or false. However, this expression cannot use any definitions in its own 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.
Note: The AllowedSubclasses and the InferiorClasses attributes require an explicit specification of a cell array of meta.class objects as their values. Other expression are not allowed.
See Specifying Attributes 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. For example, Myclass defines a constant property (Deg2Rad) and uses it in an expression that defines the default value of another property (PropA). The default value expression also uses a static method (getAngle) 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 following classes. ContClass defines the object that is created as a default property value, and ClassExp has a property that contains a ContClass object:
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 ContClass class when the ClassExp class is first used. MATLAB initializes both classes at this time. All instances of ClassExp include a copy of this same instance of ContClass.
a = ClassExp; a.ObjProp.TimeProp ans = 08-Oct-2003 17:16:08
The TimeProp property of the ContClass object contains the date and time when MATLAB initialized the class. Creating additional instances of the ClassExp class 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 TimeProp property on the object contained by ClassExp objectb:
b.ObjProp.TimeProp = datestr(now) ans = 08-Oct-2003 17:22:49
The copy of the object contained by object a is unchanged:
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 ClassExp class shows that MATLAB created an object when it initialized the ContClass and used a copy of the object handle for each instance of the ClassExp class. This means there is one ContClass object and the ObjProp property of each ClassExp object contains a copy of its handle.
Create an instance of the ClassExp class and note the time of creation:
a = ClassExp; a.ObjProp.TimeProp ans = 08-Oct-2003 17:46:01
Create a second instance of the ClassExp class. 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 TimeProp property:
b.ObjProp.TimeProp = datestr(now); b.ObjProp.TimeProp ans = 08-Oct-2003 17:47:34
Because the ObjProp property of object b contains a handle to the same object as the ObjProp property of object a, the value of the TimeProp property has changed on this object as well:
a.ObjProp.TimeProp ans = 08-Oct-2003 17:47:34
See Comparing Handle and Value Classes for more information on handle and value classes.