MATLAB Object-Oriented Programming

(Joyce) #1
(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. 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, 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 ConstantMapClass with a field ConstMapProp:

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

15 Constant Properties

Free download pdf