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存储类toAuto
orModel 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 theData Defaultstab.
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 parameterArray layoutis set to
Row-major
.Open theEmbedded Coder应用程序。
In theC Codetab, open the Code Mappings editor. SelectCode Interface>Default Code Mappings. This option pins the Code Mappings editor.
On theData Defaultstab, select theInportscategory. TheStorage Classspecified is
ExportToFile
. Click theicon and select thePreserveDimensionsproperty.To configure thePreserveDimensionsproperty 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, seePreserve Dimensions for New Storage Classes.
For a list of supported storage classes, seeStorage 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 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.
From theC Codetab, openCode Interface>Individual Element Code Mappings.
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.
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按钮。
As per the configurations in theData Defaultstab, the default storage class for signals is
Localizable
. The default configuration for signals also specifies to preserve dimensions of multidimensional arrays.By default theStorage Classof 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 underSignals/Statestab.If you want to apply the default settings, in theSignals/Statestab, set theStorage Classto
Model default: Localizable
. If you want to maintain the same storage class,Localizable
, but not preserve dimensions for the selected signal, specifyStorage ClassasLocalizable
and clear thePreserveDimensionsproperty.Note
If you specify theStorage Classas
Model 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.Click theicon , specify a signal name in theIdentifierproperty, 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]; /* '
/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 TypetoUnstructured
and 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, specifyTypeasUnstructured
and 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 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 Storage Classes, Memory Sections, and Function Templates for Software Architecture