Initialize Property Values - MATLAB & Simulink (original) (raw)

Main Content

There are two basic ways to initialize property values:

Define Properties with Default Values

You can assign a default value to an individual property using a value or an expression. Expressions cannot include variables. This example shows several ways to define a default value for a property.

classdef PropExample properties Prop1 Prop2 = "some text" Prop3 = sin(pi/12) Prop4 = datetime.empty Prop5 (1,1) double {mustBePositive} = 1 end end

Note

MATLAB evaluates a default expression when the property value is first needed (for example, when the class is first instantiated). The same default value is then used for all instances of a class. MATLAB does not reevaluate the default expression unless the class definition is cleared from memory.

Handle Objects as Default Property Values

When you use a handle class constructor to create a default property value, MATLAB calls the constructor only when the class is first used, and then uses the same object handle as the default for the property in all instances. Because all of the object handles reference the same object, any changes you make to the handle object in one instance are reflected in the handle object in all instances. To initialize a property value with a new instance of a handle object each time you instantiate your class, assign the property value in the constructor.

Set Property Values in the Constructor

To assign a value to a property from within the class constructor, refer to the object that the constructor returns (the output variable obj) and the property name using dot notation.

classdef MyClass properties Prop1 end methods function obj = MyClass(intval) obj.Prop1 = intval; end end end

When you assign a value to a property in the class constructor, MATLAB evaluates the assignment statement for each object you create. Assign property values in the constructor if you want each object to contain a unique value for that property.

For example, ContainsHandle assigns a unique handle object of classMyHandleClass to Prop1 for each instance.ContainsHandle does this by calling theMyHandleClass constructor from its own constructor.

classdef ContainsHandle properties Prop1 end methods function obj = ContainsHandle(keySet,valueSet) obj.Prop1 = MyHandleClass(keySet,valueSet); end end end

For more information on constructor methods, see Referencing the Object in a Constructor.

Property Validation Before Construction

MATLAB validates default property values before the assignment of values in the constructor. The default value assigned in the properties block and any value set for the property in the class constructor must satisfy the specified validation. For example, PropInit restrictsProp to a scalar positive double, but it does not assign a default value. By default, MATLAB assigns an initial value of empty double.

classdef PropInit properties Prop (1,1) double {mustBePositive} end methods function obj = PropInit(positiveInput) obj.Prop = positiveInput; end end end

Calling the class constructor with a valid value for Prop still causes an error because of the initial empty double inProp. An empty double does not satisfy the validation function mustBePositive.

Error using implicit default value of property 'Prop' of class 'PropInit': Value must be positive.

To avoid this problem, ensure that your properties have default values that satisfy your validation, even when you intend to overwrite those values in the constructor.