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.
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.
Refer to the constant using the class name and the property name:
ClassName.PropName
For example, to use the NamedConst
class defined in the previous section, reference the constant for the degree to radian conversion, R
:
radi = 45*NamedConst.R
radi = 0.7854
To create a library for constant values that you can access by name, first create a package 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 package called constants
:
+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 in AstroContants
:
function E = energyToOrbit(m,r) E = constants.AstroConstants.G * constants.AstroConstants.Me * m * ... (1/constants.AstroConstants.Re-0.5*r); end
Importing the package into the function eliminates the need to repeat the package name (see import
):
function E = energyToOrbit(m,r) import constants.*; E = AstroConstants.G * AstroConstants.Me * m * ... (1/AstroConstants.Re - 0.5 * r); end
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 field ConstMapProp
:
ConstMapClass.ConstMapProp('Date') = datestr(clock);
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:
p.Department
ans = Engineering
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
ans = Quality Assurance
Clearing the class results in the assignment of a new instance of MyProject
to the ProjectInfo
property.
clear MyProject
MyProject.ProjectInfo.Department
ans = Engineering
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 property PreGet
or PostGet
events. MATLAB issues a warning during class initialization if you set the GetObservable
attribute of a Constant
property to true
.