主要内容

隐藏非活动属性

R2026b

要仅显示活动的 System object™ 属性,请使用 isInactivePropertyImpl 方法。此方法用于指定某个属性是否为非活动属性。非活动属性是不因其他属性的值而影响 System object 的属性。当您将某个属性传递给 isInactiveProperty 方法,而此方法返回 true 时,则该属性为非活动属性,不会在调用 disp 函数时显示。

指定非活动属性

此示例使用 isInactiveProperty 方法检查从属属性的值。对于此 System object,如果 UseRandomInitialValue 属性设置为 true,则 InitialValue 属性是无关的。此 isInactiveProperty 方法会检查该情况,如果 UseRandomInitialValue 为 true,则返回 true 以隐藏非活动的 InitialValue 属性。

methods (Access = protected)
  function flag = isInactivePropertyImpl(obj,propertyName)
    if strcmp(propertyName,'InitialValue')
      flag = obj.UseRandomInitialValue;
    else
      flag = false;
    end
  end
end

包含非活动属性方法的完整类定义文件

classdef Counter < matlab.System
  % Counter Increment a counter
  
  % These properties are nontunable. They cannot be changed
  % after the setup method has been called or when the
  % object is running.
  properties (Nontunable)
    % Allow the user to set the initial value
    UseRandomInitialValue = true
    InitialValue = 0
  end
  
  % The private count variable, which is tunable by default
  properties (Access = private)
    pCount
  end
  
  methods (Access = protected)
    % Increment the counter and return its value 
    % as an output
    function c = stepImpl(obj)
      obj.pCount = obj.pCount + 1;
      c = obj.pCount;
    end

    % Reset the counter to either a random value or the initial
    % value.
    function resetImpl(obj)
      if obj.UseRandomInitialValue
        obj.pCount = rand();
      else
        obj.pCount = obj.InitialValue;
      end
    end

    % This method controls visibility of the object's properties
    function flag = isInactivePropertyImpl(obj,propertyName)
      if strcmp(propertyName,'InitialValue')
        flag = obj.UseRandomInitialValue;
      else
        flag = false;
      end
    end
  end
end

另请参阅