Main Content

プロパティ属性の定義

"プロパティ属性" はプロパティに詳細情報を追加して、プロパティを制御できるようにします。MATLAB® のプロパティ属性とプロパティ検証に加えて、System object は Nontunable または DiscreteState を使用できます。複数の属性を指定する場合は、属性をコンマで区切ります。

プロパティを調整不可能として指定

既定では、すべてのプロパティは "調整可能" です。つまり、プロパティの値はいつでも変更可能です。

データ処理の開始後、アルゴリズムが定数の値に依存する場合は、プロパティに Nontunable 属性を使用します。プロパティを調整不可能と定義することで、変化する値のチェックやそれらへの応答を行う必要がなくなり、アルゴリズムの効率を高められる場合があります。コード生成では、プロパティを調整不可能と定義することで、そのプロパティに関連付けられたメモリの最適化が可能になります。入力端子または出力端子の数に影響を及ぼすプロパティはすべて、調整不可能として定義します。

System object™ を使用する場合、オブジェクトを呼び出す前か関数 release を呼び出した後にのみ、調整不可能なプロパティを変更できます。たとえば、InitialValue プロパティを調整不可能として定義し、その値を 0 に設定します。

properties (Nontunable)
   InitialValue = 0;
end

プロパティを DiscreteState として指定

アルゴリズムで状態を保持するプロパティを使用している場合、これらのプロパティに DiscreteState 属性を割り当てることができます。この属性をもつプロパティは、ユーザーが getDiscreteState を呼び出すと getDiscreteStateImpl メソッドを通じて状態値を表示します。DiscreteState 属性をもつプロパティには以下の制限が適用されます。

  • 数値、論理値または fi 値 (スケーリングされた倍精度の fi 値は除く)

  • 次の属性をもたない。NontunableDependentAbstractConstant

  • 既定の値はなし

  • パブリック設定にできない

  • GetAccess = Public (既定の設定)

  • プロパティを離散状態として定義する場合は、saveObjectImplまたはloadObjectImplを使用してオブジェクトを手動で保存または上書きする必要はない。

たとえば、Count プロパティを離散状態として定義します。

properties (DiscreteState)
   Count;
end

さまざまなプロパティ属性をもつクラスの例

この例では、2 つの調整不可能なプロパティと 1 つの離散状態プロパティに加え、プロパティ属性を設定するための 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

関連するトピック