为 MATLAB 句柄类和 System object 生成代码
此示例说明如何为用户定义的 System object™ 生成代码,然后在代码生成报告中查看生成的代码。
在一个可写文件夹中,创建一个 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
编写一个使用此 System object 的函数。
function y = testAddOne(x) %#codegen p = AddOne(); y = p.step(x); end
为此代码生成一个 MEX 函数。
codegen -report testAddOne -args {0}
-report
选项指示codegen
生成一份代码生成报告,即使没有出现错误或警告也是如此。-args
选项指定testAddOne
函数接受一个标量双精度输入。点击查看报告链接。
在 MATLAB 源代码窗格中,点击
testAddOne
。要查看有关testAddOne
中变量的信息,请点击变量选项卡。要查看
addOne
的类定义,请在 MATLAB 源代码窗格中,点击AddOne
。