主要内容

可变大小数据

在生成代码中数组大小变化的代码生成

在代码生成期间,代码生成器将数组(包括标量、向量和矩阵)识别为固定大小或可变大小。

  • 如果代码生成器无法确定数组的大小或数组的大小发生变化,则代码生成器将该数组指定为可变大小。

  • 如果代码生成器可以确定数组的大小不变,则代码生成器将该数组指定为固定大小。

了解代码生成器如何处理固定大小和可变大小数组可以帮助您在代码生成期间诊断和修复错误。如果您的应用程序不需要可变大小数组,您还可以微调可变大小和动态内存分配,这可能会提高生成代码的性能。

函数

coder.areUnboundedVariableSizedArraysSupported检查当前配置设置是否允许无界可变大小数组 (自 R2024a 起)
coder.varsize解决大小不兼容性错误并声明上界

可变大小基础知识

Generate Code for Variable-Size Arrays

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

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

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

Specify Upper Bounds for Variable-Size Arrays in Generated Code

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

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

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

Generate Code for a MATLAB Function That Expands a Vector in a Loop

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

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

生成启用隐式扩展的代码

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

优化生成代码中的隐式扩展

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

生成的代码中数组的表示

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

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

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

Use Dynamically Allocated C++ Arrays in Generated Function Interfaces

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

疑难解答

解决错误:数组大小不兼容

对代码生成过程中出现的大小不兼容性错误进行故障排除。

解决错误:左侧是固定大小,而右侧是可变大小

在将可变大小数组赋给固定大小数组时对错误进行故障排除。

解决错误:字符串不支持 coder.varsize

对将 coder.varsize 与字符串变量结合使用时的代码生成错误进行故障排除。

Resolve Error: coder.varsize Not Supported for Class Properties

Troubleshoot code generation error when using coder.varsize with properties of a MATLAB class.

增大在运行时初始化为标量的可变大小列数组

Resolve Error: Incorrect Size for Expression

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.