Define Class Properties with Constant Values - MATLAB & Simulink (original) (raw)
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 therand 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:
For example, to use the NamedConst
class defined in the previous section, reference the constant for the degree to radian conversion,R
:
Constants in Namespaces
To create a library for constant values that you can access by name, first create a namespace folder, then define the various classes to organize the constants. For example, to implement a set of constants that are useful for making astronomical calculations, define a AstroConstants
class in a namespace calledconstants
:
+constants/@AstroConstants/AstroConstants.m
The class defines a set of Constant
properties with values assigned:
classdef AstroConstants properties (Constant) C = 2.99792458e8 % m/s G = 6.67259 % m/kgs Me = 5.976e24 % Earth mass (kg) Re = 6.378e6 % Earth radius (m) end end
To use this set of constants, reference them with a fully qualified class name. For example, the following function uses some of the constants defined inAstroContants
:
function E = energyToOrbit(m,r) E = constants.AstroConstants.G * constants.AstroConstants.Me * m * ... (1/constants.AstroConstants.Re-0.5*r); end
Importing the namespace into the function eliminates the need to repeat the namespace name (see import):
function E = energyToOrbit(m,r) import constants.*; E = AstroConstants.G * AstroConstants.Me * m * ... (1/AstroConstants.Re - 0.5 * r); end
Constant Property Assigned a Handle Object
If a class defines a constant property with a value that is a handle object, you can assign values to the handle object properties. To access the handle object, create a local variable.
For example, the ConstMapClass
class defines a constant property. The value of the constant property is a handle object (a containers.Map object).
classdef ConstMapClass < handle properties (Constant) ConstMapProp = containers.Map end end
To assign the current date to the Date
key, return the handle from the constant property, then make the assignment using the local variable on the left side of the assignment statement:
localMap = ConstMapClass.ConstMapProp localMap('Date') = datestr(clock);
You cannot use a reference to a constant property on the left side of an assignment statement. For example, MATLAB interprets the following statement as the creation of a struct named ConstMapClass
with a fieldConstMapProp
:
ConstMapClass.ConstMapProp('Date') = datestr(clock);
Constant Property Assigned Any Object
You can assign an instance of the defining class to a constant property. MATLAB creates the instance assigned to the constant property when loading the class. Use this technique only when the defining class is a handle class.
The MyProject
is an example of such a class:
classdef MyProject < handle properties (Constant) ProjectInfo = MyProject end properties Date Department ProjectNumber end methods (Access = private) function obj = MyProject obj.Date = datestr(clock); obj.Department = 'Engineering'; obj.ProjectNumber = 'P29.367'; end end end
Reference property data via the Constant
property:
MyProject.ProjectInfo.Date
ans =
18-Apr-2002 09:56:59
Because MyProject
is a handle class, you can get the handle to the instance that is assigned to the constant property:
p = MyProject.ProjectInfo;
Access the data in the MyProject
class using this handle:
Modify the nonconstant properties of the MyProject
class using this handle:
p.Department = 'Quality Assurance';
p
is a handle to the instance of MyProject
that is assigned to the ProjectInfo
constant property:
MyProject.ProjectInfo.Department
Clearing the class results in the assignment of a new instance ofMyProject
to the ProjectInfo
property.
clear classes MyProject.ProjectInfo.Department
You can assign an instance of the defining class as the default value of a property only when the property is declared as Constant
Constant Properties Do Not Support Events
Constant properties do not support property events. MATLAB issues a warning during class initialization if you set theGetObservable
or SetObservable
attribute of aConstant
property to true
.