Properties with Constant Values

Defining Named Constants

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 (when you first reference a constant property from that 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:

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

Constants In Packages

To create a library for constant values that you can access by name, first create a package folder, and then define the various classes to organize the constants you want to provide. 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

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's properties. However, you must create a local variable to access the handle object.

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, first return the handle from the constant property, and then make the assignment using the local variable on the left side of the assignment statement:

localMap = ConstantMapClass.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 ConstantMapClass with a field ConstMapProp:

ConstantMapClass.ConstMapProp('Date') = datestr(clock);

Constant Property Assigned Any Class Instance

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. You can 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

Was this topic helpful?