主要内容

定义属性特性

R2026b

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

将属性指定为不可调属性

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

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

注意

如果作为句柄类的 MATLAB System object™ 被分配为对象的不可调属性,则无法检测到对此 matlab.System 对象的属性的更改。但可以检测到对作为值类的 matlab.System 对象的属性的更改。如果不可调的 matlab.System 属性从一个句柄类更改为另一个句柄类,则可以被检测到。

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

properties (Nontunable)
   InitialValue = 0;
end

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

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

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

  • 没有以下任何特性:Nontunable、Dependent、Abstract、Constant。

  • 无默认值

  • 不可公开设置

  • 默认情况下 GetAccess = Public

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

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

properties (DiscreteState)
   Count;
end

插入自定义属性

使用自定义属性对话框定义具有所选特性的新属性。要添加自定义属性,请从编辑器工具条中选择插入属性下拉菜单,然后选择自定义属性...。使用该对话框为自定义属性设置属性访问、System object 特性和 MATLAB 属性特性。

访问权限

访问权限设置描述
SetAccess 和 GetAccess public

可以通过相同 System object 或引用它的另一个 System object 中的任何其他代码访问属性。

protected

只能通过相同 System object 或子类中的代码使用属性。

private

只能通过相同 System object 中的代码访问属性。

immutable

只能在创建 System object 时设置此属性值。无法更改属性值。此设置仅适用于 SetAccess。

System object 属性

属性描述
逻辑值将属性值限制为逻辑标量值。任何可以转换为逻辑值的标量值也是有效的,例如 0 或 1。
Nontunable当系统正在运行时阻止更改属性值。
DiscreteState存储状态值。
PositiveInteger将属性值限制为正整数值。

MATLAB 属性特性

属性描述
Constant此属性在类的所有实例中都只有一个值。
Hidden此属性不显示在属性列表中。
Dependent属性值不存储在对象中。set 和 get 函数无法通过使用属性名称对对象进行索引来访问属性。

要创建具有所选特性的属性,请点击插入。MATLAB 编辑器会将属性插入代码中。

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

此示例包含两个不可调属性、一个离散状态属性以及一个用于设置属性特性的 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

另请参阅

主题