Main Content

Property Syntax

Property Definition Block

The following illustration shows a typical property specification. The properties and end keywords delineate a block of code that defines properties having the same attribute settings.


Properties cannot have the same name as the class.

For an example, see Creating a Simple Class.

Assigning a Default Value

The preceding example shows the Coefficients property specified as having a default value of [0 0 1].

You can initialize property values with MATLAB® expressions. However, these expressions cannot refer to the class that you are defining in any way, except to call class static methods. MATLAB executes expressions that create initial property values only when initializing the class, which occurs just before first using the class. See Property Default Values for more information about how MATLAB evaluates default value expressions.

Define One Property Per Line

Property names must be listed on separate lines. MATLAB interprets a name following a property name as the name of a class.

Restrict Property Values

You can restrict property values by associating a class with the property in the property definition. For example, the definition of MyData requires that values assigned to this property must be of type int32 or types that are compatible with int32.

   MyData int32

For more information, see Validate Property Values.

Access Property Values

Property access syntax is like MATLAB structure field syntax. For example, if obj is an object of a class, then you can get the value of a property by referencing the property name:

val = obj.PropertyName

Assign values to properties by putting the property reference on the left side of the equal sign:

obj.PropertyName = val

When you access a property, MATLAB executes any property set or get access method and triggering any enabled property events.

Inheritance of Properties

When you derive one class from another class, the derived (subclass) class inherits all the properties of the superclass. In general, subclasses define only properties that are unique to that particular class. Superclasses define properties that are used by more than one subclass.

Specify Property Attributes

Attributes specified with the properties keyword apply to all property definitions that follow in that block. If you want to apply attribute settings to certain properties only, reuse the properties keyword and create another property block for those properties.

For example, the following code shows the SetAccess attribute set to private for the IndependentVar and Order properties, but not for the Coefficients property:

For information about the properties of a specific class, use the properties function.

Related Topics