函数定义
在创建用于定点加速的 MATLAB® 函数时,有一些特殊注意事项。例如,使用 varargin 和 varargout、递归函数、匿名函数和嵌套函数时,存在某些限制。为了生成 MATLAB 递归函数的代码,代码生成器使用编译时递归或运行时递归。为了生成高效的 C/C++ 代码,代码生成器有时会创建一个函数的多个版本,称为函数特化。在某些情况下,您可以使用 coder.ignoreConst (MATLAB Coder) 来阻止代码生成器创建函数特化。生成 MEX 函数时,您可以绕过 MATLAB 代码中特定函数的代码生成,改用 MATLAB 引擎来执行调用。为此,请使用 coder.extrinsic 构造。
函数
coder.extrinsic | 将函数声明为外部函数,并在 MATLAB 中执行它 |
coder.ignoreConst | 防止将表达式的常量值用于函数特化 |
coder.mustBeConst | 验证值为编译时常量 (自 R2023b 起) |
coder.unroll | 通过为每次循环迭代生成循环体的副本来展开 for 循环 |
coder.sameSizeBinaryOp | Apply element-wise binary operations without implicit expansion (自 R2021b 起) |
coder.noImplicitExpansionInFunction | Disable implicit expansion within the specified function in the generated code (自 R2021b 起) |
coder.read | 运行时在生成的代码中读取数据文件 (自 R2023a 起) |
coder.write | 创建生成的代码在运行时读取的数据文件 (自 R2023a 起) |
疑难解答
解决错误:数组大小不兼容 (MATLAB Coder)
对代码生成过程中出现的大小不兼容性错误进行故障排除。
解决错误:左侧是固定大小,而右侧是可变大小 (MATLAB Coder)
在将可变大小数组赋给固定大小数组时对错误进行故障排除。
解决错误:字符串不支持 coder.varsize (MATLAB Coder)
对将 coder.varsize 与字符串变量结合使用时的代码生成错误进行故障排除。
Resolve Error: coder.varsize Not Supported for Class Properties (MATLAB Coder)
Troubleshoot code generation error when using coder.varsize
with properties of a MATLAB class.
增大在运行时初始化为标量的可变大小列数组 (MATLAB Coder)
Resolve Error: Incorrect Size for Expression (MATLAB Coder)
Troubleshoot MEX function error when the size of an array at run time is incompatible with the size of the array in the generated code.