| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
How to Initialize Property Values Assigning Property Values from Within the Constructor |
You can control aspects of property definitions in the following ways:
Specifying a default value for each property individually
Assigning attribute values on a per block basis
Defining methods that execute when the property is set or queried
There are two basic approaches to initializing property values:
In the property definition — MATLAB evaluates the expression only once and assigns the same value to the property of every instance. See Defining Default Values.
In the class constructor — MATLAB evaluates the assignment expression for each instance, which ensures that each instance has a unique value. See Assigning Property Values from Within the Constructor.
Within a properties block, you can control an individual property's default value. Default values can be constant values or MATLAB expressions. Expressions cannot reference variables. For example:
classdef class_name properties PropertyName % No default value assigned PropertyName = 'some text'; PropertyName = sin(pi/12); % Expression returns default value end end
Keep in mind that the evaluation of default values occurs only once before MATLAB first instantiates the class.
MATLAB sets property values not specified in the class definition to empty ([]).
To assign values to a property from within the class constructor, reference the object that the constructor returns (the output variable obj):
classdef MyClass properties PropertyOne end methods function obj = MyClass(intval) obj.PropertyOne = intval; end end end
When you assign an object property from the class constructor, MATLAB evaluates the assignment statement for each instance created. Assign property values in the constructor if you want each object to contain a unique instance of a handle object.
See Referencing the Object in a Constructor for more information on constructor methods.
MATLAB assigns properties to the specified default values only once when MATLAB loads the class definition. Therefore, if you initialize a property value with a handle-class constructor, calls this constructor only once and every instance references the same handle object. If you want a property value to be initialized to a new instance of a handle object each time you create an object, assign the property value in the constructor.
All properties have attributes that modify certain aspects of the property's behavior. Specified attributes apply to all properties in a particular properties block. For example:
classdef class_name properties PropertyName % No default value assigned PropertyName = sin(pi/12); % Expression returns default value end properties (SetAccess = private, GetAccess = private) Stress Strain end end
In this case, only methods in the same class definition can modify and query the Stress and Strain properties. This restriction exists because the class defines these properties in a properties block with SetAccess and GetAccess attributes set to private.
Table of Property Attributes provides a description of property attributes.
You can define methods that MATLAB calls whenever setting or querying a property value. Define property set access or get access methods in methods blocks that specify no attributes and have the following syntax:
methods function value = get.PropertyName(object) ... end function obj = set.PropertyName(obj,value) ... end end
If a handle class defines the property, the set access method does not need to return the modified object.
Property Access Methods for more information on these methods.
Defining Properties for information on properties.
MATLAB can resolve a property name from a char variable using an expression of the form:
object.(PropertyNameVar)
where PropertyNameVar is a variable containing the name of a valid object property. Use this syntax when passing property names as arguments:
PN = 'KeyType'; function o = getPropValue(obj,PN) ... o = obj.(PropName); % Returns value of KeyType property ... end
![]() | The Classdef Block | Specifying Methods and Functions | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |