Main Content

Select Array Layout for Matrices in Generated Code

When generating code from a Stateflow® chart, you can specify the array layout for matrices. For example, consider this matrix:

By default, the code generator uses column-major layout to convert the matrix into a one-dimensional array and stores it in memory with this arrangement:

{1, 4, 2, 5, 3, 6}

If you select row-major layout, the code generator converts the matrix into a one-dimensional array and stores it in memory with this arrangement:

{1, 2, 3, 4, 5, 6}

If you have Embedded Coder®, you can preserve the multidimensionality of the matrix and store it as a two-dimensional array with this arrangement:

{{1, 2, 3}, {4, 5, 6}}

For more information, see Code Generation of Matrices and Arrays (Simulink Coder) and Dimension Preservation of Multidimensional Arrays (Embedded Coder).

Column-Major Array Layout

By default, the Array Layout configuration parameter for a Simulink® model is Column-Major. When you generate code from a model, the code generator flattens all matrix data into one-dimensional arrays in the column-major array layout.

For example, this Stateflow chart contains local data x of size [2 3]. The state actions index the elements in x by row and column number.

To generate code for this model:

  1. In the Apps tab, select Simulink Coder or Embedded Coder.

  2. In the C Code tab, click Build.

The file sf_matrix_layout.c implements the local data x in column-major layout with these lines of code:

...
sf_matrix_layout_DW.x[0] = 1.0;
sf_matrix_layout_DW.x[2] = 2.0;
sf_matrix_layout_DW.x[4] = 3.0;
sf_matrix_layout_DW.x[1] = 4.0;
sf_matrix_layout_DW.x[3] = 5.0;
sf_matrix_layout_DW.x[5] = 6.0;
...

The generated code refers to the elements of x by using only one index. The indices do not appear in increasing order.

Row-Major Array Layout

Row-major layout can improve the performance of certain algorithms. For example, see Interpolation Algorithm for Row-Major Array Layout (Embedded Coder).

To generate code that uses row-major array layout:

  1. Open the Configuration Parameters dialog box.

  2. In the Code Generation > Interface pane, set the Array Layout parameter to Row-Major.

  3. Generate code as described in Generate Code Using Simulink Coder (Simulink Coder) or Generate Code Using Embedded Coder (Embedded Coder).

The file sf_matrix_layout.c implements the local data x with these lines of code:

...
sf_matrix_layout_DW.x[0] = 1.0;
sf_matrix_layout_DW.x[1] = 2.0;
sf_matrix_layout_DW.x[2] = 3.0;
sf_matrix_layout_DW.x[3] = 4.0;
sf_matrix_layout_DW.x[4] = 5.0;
sf_matrix_layout_DW.x[5] = 6.0;
...

The generated code refers to the elements of x by using only one index. The indices appear in increasing order.

When you enable row-major array layout, you can pass chart and message data as arguments to custom code functions in the row-major array layout. You can also use row-major as the default layout for custom code variables. To implement row-major as the default array layout for custom code functions and variables:

  1. Open the Configuration Parameters dialog box.

  2. In the Code Generation > Interface pane, set the Array Layout parameter to Row-Major.

  3. In the Simulation Target pane, under Advanced parameters, select Import custom code.

  4. In the Import settings tab, set the Default function array layout parameter to Row-major.

You can also specify row-major array layout for individual functions. In the Simulation Target pane, in the Import settings tab, click Exception by function. In the Array Layout for Custom Code Functions window, you can add or remove functions and specify the individual array layout for each function.

If you enable row-major array layout in a chart that uses custom C code, global variables and arguments of custom code functions defined in the custom code must be scalars, vectors, or structures of scalars and vectors. Specify the size of an n-element vector as n, and not as [n 1] or [1 n].

When you enable row-major array layout in charts that use change detection operators, code generation produces an error. Before generating code in charts that use change detection operators, enable column-major array layout. See Change Detection Operators.

Multidimensional Array Layout

If you have Embedded Coder, you can generate code that preserves the multidimensionality of Stateflow data without flattening the data into one-dimensional arrays.

To generate code for the previous example using multidimensional array layout:

  1. Enable row-major layout.

  2. In the Apps tab, select Embedded Coder.

  3. In the C Code tab, select Code Interface > Default Code Mappings to open the Code Mappings editor and the Property Inspector.

  4. In the Code Mappings editor, on the Data Defaults tab, select the Signals, states, and internal data category and set the Storage Class as Localizable. If the Code Mappings editor is empty, navigate to the Simulink Model.

  5. In the Property Inspector, in the Code section, select PreserveDimensions.

  6. In the C Code tab, click Build.

The file sf_matrix_layout.c implements the local data x with these lines of code:

...
sf_matrix_layout_DW.x[0][0] = 1.0;
sf_matrix_layout_DW.x[0][1] = 2.0;
sf_matrix_layout_DW.x[0][2] = 3.0;
sf_matrix_layout_DW.x[1][0] = 4.0;
sf_matrix_layout_DW.x[1][1] = 5.0;
sf_matrix_layout_DW.x[1][2] = 6.0;
...

The generated code refers to the elements of x by using two indices.

Multidimensional array layout is available for:

  • Constant and local data in Stateflow charts

  • Message data in Stateflow charts

  • Parameters and root-level inport and outport data in Simulink models

Multidimensional layout is not available for bus signals containing multidimensional array data.

Multidimensional layout is not supported in reusable charts or charts in reusable parent subsystems.

For more information, see Preserve Dimensions of Multidimensional Arrays in Generated Code (Embedded Coder).

Multidimensional Array Limitations for Imported Buses

You cannot import a bus defined in custom code if that bus contains multidimensional arrays.

To use buses that contain multidimensional arrays in custom code, define the bus in Stateflow and pass the bus to your custom code.

For more information about defining and passing buses in custom code, see Integrate Custom Structures in Stateflow Charts.

See Also

Model Settings

Related Topics