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 函数接受一个标量双精度输入。

  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.

相关主题