主要内容

函数定义

用于代码加速的函数定义和调用

在创建用于定点加速的 MATLAB® 函数时,有一些特殊注意事项。例如,使用 vararginvarargout、递归函数、匿名函数和嵌套函数时,存在某些限制。为了生成 MATLAB 递归函数的代码,代码生成器使用编译时递归或运行时递归。为了生成高效的 C/C++ 代码,代码生成器有时会创建一个函数的多个版本,称为函数特化。在某些情况下,您可以使用 coder.ignoreConst (MATLAB Coder) 来阻止代码生成器创建函数特化。生成 MEX 函数时,您可以绕过 MATLAB 代码中特定函数的代码生成,改用 MATLAB 引擎来执行调用。为此,请使用 coder.extrinsic 构造。

函数

coder.extrinsic将函数声明为外部函数,并在 MATLAB 中执行它
coder.ignoreConst防止将表达式的常量值用于函数特化
coder.mustBeConst验证值为编译时常量 (自 R2023b 起)
coder.unroll通过为每次循环迭代生成循环体的副本来展开 for 循环
coder.sameSizeBinaryOpApply element-wise binary operations without implicit expansion (自 R2021b 起)
coder.noImplicitExpansionInFunctionDisable implicit expansion within the specified function in the generated code (自 R2021b 起)
coder.read运行时在生成的代码中读取数据文件 (自 R2023a 起)
coder.write创建生成的代码在运行时读取的数据文件 (自 R2023a 起)

可变大小基础知识

Generate Code for Variable-Size Arrays (MATLAB Coder)

Learn how the code generator defines and handles fixed- and variable-size data.

为具有 end + 1 索引的增大数组和元胞数组生成代码 (MATLAB Coder)

用简单的语法在 MATLAB 代码中增大数组和元胞数组。

Specify Upper Bounds for Variable-Size Arrays in Generated Code (MATLAB Coder)

To avoid dynamic memory allocation, specify upper bounds for variable-size arrays.

在代码生成的可变大小支持方面与 MATLAB 的不兼容性 (MATLAB Coder)

了解可变大小数据如何导致生成代码的行为与 MATLAB 不同。

Generate Code for a MATLAB Function That Expands a Vector in a Loop (MATLAB Coder)

Generate C and C++ code for a function that uses variable-size data with and without dynamically allocated memory.

动态内存分配

Control Dynamic Memory Allocation in Generated Code (MATLAB Coder)

Balance memory usage and performance by controlling dynamic memory allocation for fixed- and variable-size arrays.

生成启用隐式扩展的代码 (MATLAB Coder)

代码生成器在生成的代码中引入修改以实现隐式扩展。

优化生成代码中的隐式扩展 (MATLAB Coder)

默认情况下,生成代码中的隐式扩展是启用的。代码生成器在生成的代码中引入修改以执行隐式扩展。生成的代码中的更改可能导致额外的代码来扩展操作数。操作数的扩展可能会影响生成代码的性能。请参阅生成启用隐式扩展的代码 (MATLAB Coder)。

生成的代码中数组的表示 (MATLAB Coder)

了解数组在生成的代码中是如何表示的。

在生成的函数接口中使用动态分配的 C 数组 (MATLAB Coder)

了解并使用生成的 C 函数接口中的动态分配数组。

Use Dynamically Allocated C++ Arrays in Generated Function Interfaces (MATLAB Coder)

Understand and use dynamically allocated arrays from the generated C++ function interfaces.

疑难解答

解决错误:数组大小不兼容 (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.