MATLAB Object-Oriented Programming

(Joyce) #1

Define Class Properties with Constant Values


In this section...
“Defining Named Constants” on page 15-2
“Constant Property Assigned a Handle Object” on page 15-4
“Constant Property Assigned Any Object” on page 15-4
“Constant Properties — No Support for Get Events” on page 15-6

Defining Named Constants


You can define constants that you can refer to by name by creating a MATLAB class that
defines constant properties.

Use constant properties to define constant values that you can access by name. Create a
class with constant properties by declaring the Constant attribute in the property
blocks. Setting the Constant attribute means that, once initialized to the value specified
in the property block, the value cannot be changed.

Assigning Values to Constant Properties

Assign any value to a Constant property, including a MATLAB expression. For example:

classdef NamedConst
properties (Constant)
R = pi/180
D = 1/NamedConst.R
AccCode = '0145968740001110202NPQ'
RN = rand(5)
end
end

MATLAB evaluates the expressions when loading the class. Therefore, the values
MATLAB assigns to RN are the result of a single call to the rand function and do not
change with subsequent references to NamedConst.RN. Calling clear classes causes
MATLAB to reload the class and reinitialize the constant properties.

Referencing Constant Properties

Refer to the constant using the class name and the property name:

15 Constant Properties

Free download pdf