Main Content

定义属性特性

属性特性用于添加属性详细信息,可为属性提供控制层。除了 MATLAB® 属性特性和属性验证,System object 还可以使用 NontunableDiscreteState。要指定多个特性,请用逗号分隔它们。

将属性指定为不可调属性

默认情况下,所有属性均为可调的,这意味着属性值可以随时更改。

当算法依赖于数据处理开始后始终恒定的值时,则要为属性应用 Nontunable 特性。将属性定义为不可调属性后,不需要检查或响应可变值,从而可以提高算法的效率。对于代码生成,将某个属性定义为不可调属性可以优化与该属性关联的内存。您应该将影响输入或输出端口数量的所有属性都定义为不可调属性。

使用 System object™ 时,只能在调用对象之前或调用 release 函数之后更改不可调属性。例如,您将 InitialValue 属性定义为 nontunable,并将其值设置为 0。

properties (Nontunable)
   InitialValue = 0;
end

将属性指定为离散状态属性

如果您的算法使用可保留状态的属性,则可以为这些属性分配 DiscreteState 特性。当用户调用 getDiscreteState 时,具有此特性的属性将通过 getDiscreteStateImpl 显示其状态值。以下限制适用于带有 DiscreteState 特性的属性,

  • 数值、逻辑值或 fi 值,但非定标的双精度 fi 值

  • 没有以下任何特性:NontunableDependentAbstractConstant

  • 无默认值

  • 不可公开设置

  • 默认情况下 GetAccess = Public

  • 如果将属性定义为离散状态,则不需要使用 saveObjectImplloadObjectImpl 手动保存或覆盖对象。

例如,您将 Count 属性定义为离散状态:

properties (DiscreteState)
   Count;
end

具有各种属性特性的类示例

此示例包含两个不可调属性、一个离散状态属性以及一个用于设置属性特性的 MATLAB 类属性验证。

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

相关主题