Define Property Attributes
Property attributes, which add details to a property, provide a
layer of control to your properties. In addition to the MATLAB® property attributes and property validation, System objects can use
Nontunable
or DiscreteState
. To specify multiple
attributes, separate them with commas.
Specify Property as Nontunable
By default all properties are tunable, meaning the value of the property can change at any time.
Use the Nontunable
attribute for a property when the algorithm
depends on the value being constant once data processing starts. Defining a property as
nontunable may improve the efficiency of your algorithm by removing the need to check for or
react to values that change. For code generation, defining a property as nontunable allows
the memory associated with that property to be optimized. You should define all properties
that affect the number of input or output ports as nontunable.
When you use the System object™, you can only change nontunable properties before calling the object or after
calling the release
function. For example, you define the
InitialValue
property as nontunable and set its value to 0.
properties (Nontunable)
InitialValue = 0;
end
Specify Property as DiscreteState
If your algorithm uses properties that hold state, you can assign those properties the
DiscreteState
attribute. Properties with this attribute display their
state values via the getDiscreteStateImpl
when users call
getDiscreteState
. The following restrictions apply to a property with
the DiscreteState
attribute,
Numeric, logical, or fi value, but not a scaled double fi value
Does not have any of these attributes:
Nontunable
,Dependent
,Abstract
,Constant
.No default value
Not publicly settable
GetAccess = Public
by defaultIf you define the property as discrete state, you do not need to manually save or overwrite the object using
saveObjectImpl
orloadObjectImpl
.
For example, you define the Count
property as a discrete
state:
properties (DiscreteState)
Count;
end
Insert Custom Property
Use the Custom Property dialog box to define a new property with selected attributes. To add custom property, select the Insert Property drop-down from the Editor toolstrip and select Custom Property.... Use the dialog box to set the property access, System object attributes, and MATLAB property attributes for your custom properties.
Access
Access | Setting | Description |
---|---|---|
SetAccess and GetAccess
| public | Property can be accessed by any other code in the same System object or another System object that references it. |
protected | Property can be used only by code in the same System object or in a subclass. | |
private | Property can be accessed only by code in the same System object. | |
immutable | You can set this property value only when you create the System object. You cannot change the property value. This setting applies only to
|
System Object Attributes
Attribute | Description |
---|---|
Logical | Limits the property values to logical scalar values. Any scalar value that can be converted to a logical is also valid, such as 0 or 1. |
Nontunable | Prevents changes to the property values while system is running. |
DiscreteState | Holds state value. |
PositiveInteger | Limits the property value to a positive integer value. |
MATLAB Property Attributes
Attribute | Description |
---|---|
Constant | This property has only one value in all instances of the class. |
Hidden | The property is not shown in a property list. |
Dependent | The property value is not stored in the object. The set and
get functions cannot access the property by indexing into the
object using the property name. |
To create the property with the selected attributes, click Insert. MATLAB Editor inserts the property into your code.
Example Class with Various Property Attributes
This example shows two nontunable properties, a discrete state property, and also MATLAB class property validation to set property attributes.
classdef Counter < matlab.System % Counter Increment a counter to a maximum value % These properties are nontunable. They cannot be changed % after the setup method has been called or while the % object is running. properties (Nontunable) % The initial value of the counter InitialValue = 0 % The maximum value of the counter, must be a positive integer scalar MaxValue (1, 1) {mustBePositive, mustBeInteger} = 3 end properties % Whether to increment the counter, must be a logical scalar Increment (1, 1) logical = true end properties (DiscreteState) % Count state variable Count end methods (Access = protected) % Increment the counter and return its value % as an output function c = stepImpl(obj) if obj.Increment && (obj.Count < obj.MaxValue) obj.Count = obj.Count + 1; else disp(['Max count, ' num2str(obj.MaxValue) ' ,reached']) end c = obj.Count; end % Setup the Count state variable function setupImpl(obj) obj.Count = 0; end % Reset the counter to one. function resetImpl(obj) obj.Count = obj.InitialValue; end end end