Main Content

setup

System object の 1 回限りの設定タスク

説明

setup(obj) は、System object™ に固有の 1 回限りの設定タスクを実行します。

setup(obj,input1,...,inputN) は、その設定タスクで入力値を検証するためにサンプル入力を必要とする場合、1 回限りの設定タスクを実行します。

Counter System object の初期化

この例では、System object™ について setup を呼び出す方法を説明します。多くの場合、System object の初回実行時に setup の初期化が行われるため、setup を直接呼び出す必要はありません。初期化の実行時間についての懸念がある場合にのみ、実行の前に setup を呼び出します。

System object Counter を、開始値 5 で作成します (Counter の完全な定義については以下の節を参照してください)。

count = Counter('StartValue',5)
count = 
  Counter with properties:

    UseIncrement: true
    UseWrapValue: true
      StartValue: 5
       Increment: 1
       WrapValue: 10

Counter オブジェクトの定義で、setupImplStartValue プロパティを、指定されたカウント開始の数で初期化します。setup を呼び出すと、System object は setupImpl を呼び出し、入力値とプロパティ値の検証も行います。Counter がこれらの内部検証メソッドを定義しているため、setup に検証する入力値を与えなければなりません。

プレースホルダー入力値を使用して setup を呼び出して、count オブジェクトの StartValue を初期化します。初期化の後、オブジェクトを実行します。

setup(count,0)
count(2)
ans = 7

Counter System object の完全な定義

type Counter.m
classdef Counter < matlab.System
% COUNTER Compute an output value by incrementing the input value
  
  % All properties occur inside a properties declaration.
  % These properties have public access (the default)
  properties
    UseIncrement (1,1) logical = true    % Use custom increment value.
    UseWrapValue (1,1) logical = true    % Use max value.
    
    StartValue (1,1) {mustBeInteger,mustBePositive} = 1   % Value to start from.
    Increment (1,1) {mustBeInteger,mustBePositive} = 1    % What to add to Value every step.
    WrapValue (1,1) {mustBeInteger,mustBePositive} = 10   % Max value to wrap around.
  end

  properties(Access = protected)
      Value
  end

  methods
    % Constructor - Support name-value pair arguments when constructing object
    function obj = Counter(varargin)
        setProperties(obj,nargin,varargin{:})
    end

    function set.Increment(obj,val)
        if val >= 10
          error('The increment value must be less than 10');
        end
        obj.Increment = val;
    end
    
  end
  
  methods (Access = protected)
    % Validate the object properties  
    function validatePropertiesImpl(obj)
        if obj.UseIncrement && obj.UseWrapValue && ...
                (obj.WrapValue < obj.Increment)
          error('Wrap value must be greater than increment value');
        end
    end
    
    % Validate the inputs to the object
    function validateInputsImpl(~,x)
        if ~isnumeric(x)
          error('Input must be numeric');
        end
    end
    
    % Perform one-time calculations, such as computing constants
    function setupImpl(obj)
        obj.Value = obj.StartValue;
    end
  
    % Step
    function out = stepImpl(obj,in)
      if obj.UseIncrement
        % If using increment property, multiple the increment by the input.
        obj.Value = in*obj.Increment + obj.Value;
      else
         % If not using increment property, add the input.
        obj.Value = in + obj.Value;
      end
      if obj.UseWrapValue && obj.Value > obj.WrapValue
         % If UseWrapValue is true, wrap the value
         % if it is greater than the WrapValue.
         obj.Value = mod(obj.Value,obj.WrapValue);
      end
      out = obj.Value;
    end
  end
end

他のツールボックスの例

入力引数

すべて折りたたむ

System object を実行する前に設定する System object

代替機能

多くの System object では、setup を呼び出す必要はありません。System object の初回呼び出し時に setup が呼び出されます (呼び出しシーケンスの概要を参照してください)。初期化の計算負荷を軽減する必要がある場合にのみ setup を別個に呼び出します。

バージョン履歴

R2010a で導入