Main Content

MATLAB のハンドル クラスおよび System object のコード生成

この例では、ユーザー定義の System object™ のコードを生成する方法について説明し、コード生成レポートで生成されたコードを示します。

  1. 書き込み可能なフォルダーに System object AddOne を作成します。これは matlab.System からサブクラス化されるものです。AddOne.m としてコードを保存します。

    classdef AddOne < matlab.System
    % ADDONE Compute an output value that increments the input by one
    
      methods (Access=protected)
        % stepImpl method is called by the step method
        function y = stepImpl(~,x)
          y = x+1;
        end
      end
    end	  
  2. この System object を使用する関数を記述します。

    function y = testAddOne(x)
    %#codegen
      p = AddOne();
      y = p.step(x);
    end    
  3. このコードの MEX 関数を生成します。

    codegen -report testAddOne -args {0}

    -report オプションにより、エラーや警告が発生しなくても、codegen でコード生成レポートを生成することを指定します。-args オプションにより、testAddOne 関数が 1 つのスカラーで double の入力を受け取ることを指定します。

  4. [レポートの表示] リンクをクリックします。

  5. [MATLAB ソース] ペインで、testAddOne をクリックします。testAddOne の変数の詳細を表示するには、[変数] タブをクリックします。

    This shows the MATLAB function testAddOne in the report. The cursor points to the variable p and the properties display.

  6. addOne のクラス定義を表示するには、[MATLAB ソース] ペインで、AddOne をクリックします。

    This image shows the class definition for addOne in the report.

関連するトピック