Why is my 2-D array data flattened in code generation from my Simulink model using Embedded Coder but not when I use a Stateflow chart?

I am generating code for my model. When generating code normally, my 2-D array or matrix is flattened as shown below:
var1 = ((&my2DArray[0][0]))[4];
However, when my signals, which are handling the 2-D matrix data, are passed through a Stateflow chart in my model first, the data is represented as a 2-D array even in my generated C-code, as shown below:
var1 = my2DArray[1][2];
Why is my code generated with single indexing for matrices and 2-D arrays, but not when using a Stateflow?

 采纳的回答

The "flattening", the process by which a 2-D array or matrix is transformed into a 1-Dimensional array, is intended behaviour in code generation. 
In generated C code, all matrices are stored as linear memory; whether the code uses A[i][j] or linear pointer-based indexing is a representation choice that depends on whether the array dimensions are fully known at code‑generation time.
Why does Stateflow behave differently?
Stateflow defines matrices explicitly at/before build time, as outlined in the sizing Stateflow data documentation.
Because the sizes are known at compile‑time, the code generator can emit a true C multidimensional array and use natural 2‑D indexing, such as A[i][j].
The way vectors and matrices are defined in code generated for Stateflow charts is described in the Stateflow operations for vectors and matrices, and the how vectors and matrices work in Stateflow documentation pages.
Why is it represented as 1-dimensional otherwise?
This is intended behaviour. Within your non-Stateflow workflow, dimensions cannot be guaranteed to be compile‑time constants.
This means that the code generator cannot declare a true C 2‑D array type. In this situation, it linearises matrix access via row-major or column-major 1-dimensional array representation.
The way matrices and arrays are represented in generated code is explained further in the code generation of matrices and arrays documentation.

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Deployment, Integration, and Supported Hardware 的更多信息

产品

版本

R2025b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by