MATLAB Object-Oriented Programming

(Joyce) #1

Properties


In this section...
“The Properties Block” on page 5-12
“Access to Property Values” on page 5-13

The Properties Block


Define class properties within a properties block:

classdef ClassName
properties (PropertyAttributes)
PropertyName class = DefaultValue
end
end

Property attributes apply to all properties defined within the block. To define properties
with different attributes, use multiple properties block. All property attributes have
default values. For a list of property attributes, see “Property Attributes” on page 8-9.

Optionally assign default values to the property in the properties block. MATLAB
evaluates the assignment statement when the class is first referenced or when loading a
saved object.

NoteEvaluation of property default values occurs only when the value is first needed,
and only once when MATLAB first initializes the class. MATLAB does not reevaluate the
expression each time you create an instance of the class.

For more information on the evaluation of expressions that you assign as property default
values, see “When MATLAB Evaluates Expressions” on page 6-13.

Properties with Different Attributes

The following class defines three properties. Model and Color use default attribute
values, resulting in public read and write access. SerialNumber has read-only access by
object users. Assign the SerialNumber property value from a class member function,
such as the constructor or other class method.

5 Class Definition—Syntax Reference

Free download pdf