Expression Evaluation in Handle and Value Classes
The following example shows how value and handle object behave when assigned to
properties as default values. Suppose that you have the following classes.Expressions in Value ClassesThe ClassExp class has a property that contains a ContClass object:classdef ContClass
properties
% Assign current date and time
TimeProp = datestr(now)
end
endclassdef ClassExp
properties
ObjProp = ContClass
end
endWhen you first use the ClassExp class, MATLAB creates an instance of the ContClass
class. MATLAB initializes both classes at this time. All instances of ClassExp include a
copy of this same instance of ContClass.a = ClassExp;
a.ObjProp.TimePropans =08-Oct-2003 17:16:08The 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.TimePropans =08-Oct-2003 17:16:086 Defining and Organizing Classes