Main Content

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 theArray layoutparameter toRow-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, seeDimension 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 theCode Mappings>Data Defaultstab, you can configure these model data elements categories. You specify a storage class to preserve dimensions.

  • Inports

  • Outports

  • Signals, states, and internal data

  • Shared local data stores

  • Global data stores

  • Model parameters

  • External parameters

Then, when you set the存储类toAutoorModel defaulton individual model data elements, by default, the code generator applies the configuration that you set up for the respective model data elements in theData Defaultstab.

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

  1. Open thertwdemo_preservedimensionsexample model.

  2. In the Configuration Parameters dialog box, verify that parameterArray layoutis set toRow-major.

  3. Open theEmbedded Coder应用程序。

  4. In theC Codetab, open the Code Mappings editor. SelectCode Interface>Default Code Mappings. This option pins the Code Mappings editor.

  5. On theData Defaultstab, select theInportscategory. TheStorage Classspecified isExportToFile. Click theIcon to configure additional code mapping propertiesicon and select thePreserveDimensionsproperty.

    To configure thePreserveDimensionsproperty programmatically, use thecoder.mapping.defaults.setfunction in the MATLAB command window:

    coder.mapping.defaults.set('rtwdemo_preservedimensions','Inports',...'Storageclass','ExportToFile',...'PreserveDimensions','off')

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

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

  6. Generate and review code. The generated code preserves the dimensions of 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 theData Defaultstab by configuring individual model data element sources in other tabs such asInports,Outports,Parameters,数据存储, andSignals/States. Here is an example workflow that is applicable to the model data element categories.

  1. From theC Codetab, openCode Interface>Individual Element Code Mappings.

  2. In the Code Mappings editor, select theSignals/Statestab. TheSignalssection is currently empty. There can be multiple signals in a given model. Some of these signals might not be important when configuring code generation. You can mark signals as signals of interest that are relevant to your code generation configuration. When you add a signal explicitly, it gets added to the Code Mappings editor.

    In the Code Mappings editor, open theSignals/Statestab. Select the signal in the Simulink®Editor that you want to add to the Code Mappings editor. Pause on the ellipsis (...) that appears above or below the signal line to open the action bar. Click theAdd selected signals to code mappings按钮。信号是阿宝pulated in theSignalstable in the Code Mappings editor.Signal of interest is selected in the model and the action bar is displayed. The cursor is on the Enable Code Configuration option.

    For this example, select the signal going from the subsystem Volume Controller to the outport Actuator Command and click theAdd selected signals to code mappings按钮。

  3. As per the configurations in theData Defaultstab, the default storage class for signals isLocalizable. The default configuration for signals also specifies to preserve dimensions of multidimensional arrays.

    By default theStorage Classof the individually added signal is configured asAuto. You can override the default configuration for the individual signal by changing the specification of the added signal underSignals/Statestab.

    If you want to apply the default settings, in theSignals/Statestab, set theStorage ClasstoModel default: Localizable. If you want to maintain the same storage class,Localizable, but not preserve dimensions for the selected signal, specifyStorage ClassasLocalizableand clear thePreserveDimensionsproperty.

    Note

    If you specify theStorage ClassasModel default, you cannot override the default configurations specified inData Defaultstab. To override the default specifications for the default storage class, select that storage class and change the configurations for the individual modeling element.

  4. Click theIcon to configure additional code mapping propertiesicon , specify a signal name in theIdentifierproperty, such asVolumeController.

  5. Generate and review code. The generated code does not preserve dimensions of the individual signal but does for other signals.

    /* Definition for custom storage class: Localizable */ static real_T LogSignal[11][9]; /* '/Log volume difference' */real_T VolumeController[6]; /* '/Gain' */static real_T rtIterationCount[2][3]; /* '/IterationCount' */ static real_T rtIterationCount_g[2][3];/* '/PreviousVolumeErrorAverage' */ static real_T rtVolumedifference[2][3];/* '/Volume difference' */

Storage Classes Supported to Preserve Dimensions

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

  • Const

  • Volatile

  • ConstVolatile

  • ExportToFile

  • ImportFromFile

  • FileScope

  • Localizable

Preserve Dimensions for New Storage Classes

To preserve dimensions when you design your own storage class, in the Embedded Coder Dictionary, in the Property Inspector, setStorage TypetoUnstructuredand select thePreserve array dimensionsproperty. Selecting this property preserves dimensions for instances of your storage class. To use the storage class, in the Code Mappings editor, configure the model element categories by specifyingStorage Classto the new storage class that you created.

To preserve dimensions when you design your own storage class in the Custom Storage Class Designer, specifyTypeasUnstructuredand select thePreserve array dimensionsproperty.Preserve array dimensionshas these options:

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

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

  • Instance Specific— If you want to preserve array dimensions for each instance of the storage class, enable thePreserve array dimensionsproperty on the parameter object. For the Simulink package, this property setting isInstance Specificby default.

Limitations

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

See Also

||

Related Topics