Initialize Property Values

There are two basic ways to initialize property values:

• Define properties with default values — MATLAB® assigns the same initial value to the property of every instance.

• Set property values in the constructor — The constructor evaluates the assignment statement for each instance, which enables instances to have unique initial 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```
• `Prop1` — The property definition does not specify a default value, so MATLAB initializes the property value to an empty `double` (`[]`).

• `Prop2` — The default value is the string scalar `"some text"`.

• `Prop3` — The default value is the value of `sin(pi/12)`. Reading this property returns the evaluated expression (`0.2588`), not the expression itself.

For more information on the evaluation of expressions that you assign as default values, see Evaluation of Expressions in Class Definitions and Properties Containing Objects.

• `Prop4` — The default value is an empty `datetime` object.

• `Prop5` — The default value is `1`, and the property value in general is restricted to scalar, positive doubles. When a property definition specifies any size, class, or validation function restrictions on the property value, then the default value must satisfy those conditions. For example, a default value of 0 would cause an error during instantiation because it does not satisfy `mustBePositive`.

For information on property restrictions based on size, class, and validation functions, see Validate Property Values.

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 class `MyHandleClass` to `Prop1` for each instance. `ContainsHandle` does this by calling the `MyHandleClass` 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` restricts `Prop` 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 in `Prop`. An empty double does not satisfy the validation function `mustBePositive`.

`obj = PropInit(2);`
```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.