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 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
Signals, states, and internal data
Shared local data stores
Global data stores
Model parameters
External parameters
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 in the Data Defaults
tab.
An example workflow that is applicable to the preceding model data element categories is:
Open the
rtwdemo_preservedimensions
example model.In the Configuration Parameters dialog box, verify that parameter Array layout is set to
Row-major
.Open the Embedded Coder app.
In the C Code tab, open the Code Mappings editor. Select Code Interface > Default Code Mappings. This option pins the Code Mappings editor.
On the Data Defaults tab, select the Inports category. The Storage Class specified is
ExportToFile
. Click theicon and select the PreserveDimensions property.
To configure the PreserveDimensions property programmatically, use the
coder.mapping.defaults.set
function 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, see Preserve Dimensions for New Storage Classes.
For a list of supported storage classes, see Storage Classes Supported to Preserve Dimensions.
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 the Data Defaults tab by configuring individual model data element sources in other tabs such as Inports, Outports, Parameters, Data Stores, and Signals/States. Here is an example workflow that is applicable to the model data element categories.
From the C Code tab, open Code Interface > Individual Element Code Mappings.
In the Code Mappings editor, select the Signals/States tab. The Signals section 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 the Signals/States tab. 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 the Add selected signals to code mappings button. The signal is populated in the Signals table in the Code Mappings editor.
For this example, select the signal going from the subsystem Volume Controller to the outport Actuator Command and click the Add selected signals to code mappings button.
As per the configurations in the Data Defaults tab, the default storage class for signals is
Localizable
. The default configuration for signals also specifies to preserve dimensions of multidimensional arrays.By default the Storage Class of the individually added signal is configured as
Auto
. You can override the default configuration for the individual signal by changing the specification of the added signal under Signals/States tab.If you want to apply the default settings, in the Signals/States tab, set the Storage Class to
Model default: Localizable
. If you want to maintain the same storage class,Localizable
, but not preserve dimensions for the selected signal, specify Storage Class asLocalizable
and clear the PreserveDimensions property.Note
If you specify the Storage Class as
Model default
, you cannot override the default configurations specified in Data Defaults tab. To override the default specifications for the default storage class, select that storage class and change the configurations for the individual modeling element.Click the
icon , specify a signal name in the Identifier property, such as
VolumeController
.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]; /* '<S2>/Log volume difference' */ real_T VolumeController[6]; /* '<S3>/Gain' */ static real_T rtIterationCount[2][3]; /* '<S2>/IterationCount' */ static real_T rtIterationCount_g[2][3];/* '<S2>/PreviousVolumeErrorAverage' */ static real_T rtVolumedifference[2][3];/* '<S1>/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
GetSet
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, set Storage Type
to Unstructured
and select the Preserve array
dimensions property. 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 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, specify Type as Unstructured
and select 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 storage class.Instance Specific
— If you want to preserve array dimensions for each instance of the storage class, enable the Preserve array dimensions property on the parameter object. For the Simulink package, this property setting isInstance Specific
by default.
Limitations
The code generator does not preserve dimensions for parameters without data objects.
See Also
Code Mappings Editor –
C | coder.mapping.api.CodeMapping
| Embedded Coder Dictionary
Related Topics
- Code Generation of Matrices and Arrays
- Select Array Layout for Matrices in Generated Code (Stateflow)
- Preserve Dimensions of Bus Elements in Generated Code
- Choose Storage Class for Controlling Data Representation in Generated Code
- Define Service Interfaces, Storage Classes, Memory Sections, and Function Templates for Software Architecture