Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Defining Named Constants

A Class to Define Named Constants

You can define a collection of constants whose values you can access by name. Create a class having properties with their Constant attribute set to true. Setting the Constant attribute to true effectively sets the SetAccess to private, so that the values of the properties cannot be changed outside of the class.

You might define a package of classes defining various sets of constants and import these classes into any function that needs them.

Assigning Values to Constant Properties

You can assign any value to a Constant property, including a MATLAB expression. For example:

classdef NamedConst
   properties (Constant)
      R = pi/180;
      D = 1/RadDeg.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). This means the values assigned to RN are the result to 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.

Referencing Constant Properties

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

ClassName.PropName

For example, to use the RadDeg class defined in the previous section:

radi = 45*RadDeg.R

radi =

    0.7854

A Package for Constants

To create a library for constant values that you can access by name, first create a package directory, and then define the various classes to organize the constants you want to provide. For example, to implement a set of constants used for making astronomical calculations, you might define a AstroConstants class in a package called Constants:

+Constants/@AstroConstants/AstroConstants.m

The class defines on a set of Constant properties with initial 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, you need to 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

Note that you cannot import class properties. Therefore, you must use the fully qualified class name to reference constant properties.

Setting Constant Property Default

You can define a Constant property default value as the class that defines the property. MATLAB creates the instance assigned to the Constant property when loading the class. The MyCnstClass shows the behavior of Constant properties that contain an instance of the defining class:

classdef MyCnstClass
   properties (Constant)
      Instance = MyCnstClass;
   end
   properties
      Date
   end
   methods (Access = private)
      function obj = MyCnstClass
         obj.Date = clock;
      end
   end
end

Reference the class instance contained in the Constant property:

MyCnstClass.Instance.Date

ans =

  1.0e+003 *

    2.0090    0.0070    0.0080    0.0110    0.0440    0.0331
clear
>> MyCnstClass.Instance.Date

ans =

  1.0e+003 *

    2.0090    0.0070    0.0080    0.0110    0.0440    0.0331
clear classes
MyCnstClass.Instance.Date

ans =

  1.0e+003 *

    2.0090    0.0070    0.0080    0.0110    0.0450    0.0419

For properties that are not Constant, you cannot use a class instance for a default value.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS