Preserve Dimensions of Multidimensional Arrays in Generated Code

By default, the generated code contains one-dimensional arrays for multidimensional model data. In the Configuration Parameters dialog box, if you set the Array layout (Simulink Coder) parameter to Row-major, you can preserve dimensions of multidimensional arrays in the generated code. Preserving array dimensions in the generated code enhances integration with external code.

MATLAB® and C organize multidimensional data in different ways. For more details, see Dimension Preservation of Multidimensional Arrays.

In the generated code, you can preserve dimensions of root-level inports and outports, parameters, signals, states, lookup tables, and data stores.

Configure Default Model Data Elements to Preserve Dimensions

In the Code Mappings > Data Defaults tab, you can configure these model data elements categories. You specify a storage class to preserve dimensions.

  • Inports

  • Outports

  • Model parameters

  • External parameter objects

  • Shared local data stores

  • Global data stores

  • Internal data

Then, when you set the Storage class to Auto or Model default on individual model data elements, by default, the code generator applies the configuration that you set up for the respective model data elements.

An example workflow that is applicable to all preceding model data element categories is:

  1. Open the model rtwdemo_preservedimensions.

  2. In the Configuration Parameters dialog box, verify that parameter Array layout is set to Row-major.

  3. From the Simulink toolstrip, on the Apps tab, select the Embedded Coder app.

  4. In the C Code tab, open the Code Mappings editor.

  5. In the Code Mappings editor, select the Data Defaults tab.

    Select the Inports category. The Storage Class specified is ExportToFile. In the Property Inspector, in the Code section, select the PreserveDimensions property.

    Property Inspector window attached to the Code Mappings editor. Shows
                            the selected PreserveDimensions property check box

    You can create a new storage class in Embedded Coder®, and then specify that class to preserve dimensions. For more details, see Preserve Dimensions for New Storage Classes.

    For a list of supported storage classes, see Storage Classes Supported to Preserve Dimensions.

  6. Generate code. The generated code preserves the dimensions of all the root-level Inports.

    real_T rtPressure[2][3];
    real_T rtTargetVolume[2][3];
    real_T rtTemperature[2][3]; 

Configure Individual Model Data Elements to Preserve Dimensions

You can override the default configurations specified in the Code Mappings editor by configuring individual model data element sources in the Model Data Editor. Here is an example workflow that is applicable to all model data element categories.

  1. In the Modeling tab, click the Model Data Editor tab.

  2. In the Model Data Editor, click the Show/refresh additional information button.

  3. In the Model Data Editor, select the Inports/Outports tab.

  4. Set the Change view to Code.

  5. In the subsystem Volume Controller, select the signal Volume difference and set the Storage Class to ExportToFile or any other supported storage class. For details, see Storage Classes Supported to Preserve Dimensions.

  6. Keeping the row in focus, open the Property Inspector. In the Code section, select the Preserve array dimensions property.

    Property Inspector window attached to the Model Data Editor. Shows the
                            selected Preserve array dimensions property check box

  7. Generate code. The generated code preserves the dimensions of the signal.

    real_T Volumedifference[2][3];

Storage Classes Supported to Preserve Dimensions

You can preserve dimensions in the generated code for these storage classes:

  • Const

  • Volatile

  • ConstVolatile

  • ExportToFile

  • ImportFromFile

  • FileScope (not supported for Global data store)

  • Localizable

Preserve Dimensions for New Storage Classes

To preserve dimensions when you design your own storage class, in the Embedded Coder Dictionary, select the Preserve array dimensions option in the Property Inspector. Selecting this property preserves dimensions for all instances of your storage class. To use the storage class, in the Code Mappings editor, configure the model element categories by specifying Storage Class to the new storage class that you created.

To preserve dimensions when you design your own storage class in the Custom Storage Class Designer, use the Preserve array dimensions property. Preserve array dimensions has these options:

  • No (default) — Flattens the multidimensional array to one dimension in the generated code.

  • Yes — Preserves array dimensions for parameters of the specified custom storage class.

  • Instance Specific — If you want to preserve array dimensions for each instance of the custom storage class, enable the Preserve array dimensions property on the parameter object. For the Simulink package, this property setting is Instance Specific by default.

Limitations

  • The code generator flattens multidimensional arrays in Simulink® buses in the generated code.

  • You cannot preserve array dimensions for MPT parameter objects.

  • The code generator does not preserve dimensions for signal objects when you apply a custom package to the signal object and rename the storage class that supports dimension preservation.

  • The code generator does not preserve dimensions for parameters without data objects.

Related Topics