Skip to Main Content Skip to Search
Product Documentation

Properties with Constant Values

Defining Named Constants

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

You can 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). Therefore, the values MATLAB assigns 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, reference the constant for the degree to radian conversion, R:

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 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, you can define a AstroConstants class in a package called Constants:

+Constants/@AstroConstants/AstroConstants.m

The class defines 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, 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

You cannot import class properties. Therefore, 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-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS