MATLAB Object-Oriented Programming

(Joyce) #1
For an example, see “Create a Simple Class” on page 2-2.

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” on page 8-18 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.

properties
MyData int32
end

For more information, see “Validate Property Values” on page 8-24.

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

8 Properties — Storing Class Data

Free download pdf