Preserve Dimensions of Bus Elements 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. See Preserve Dimensions of Multidimensional Arrays in Generated Code.
In the generated code, you can preserve dimensions of root-level inports and outports, parameters, signals, states, lookup tables, and data stores by configuring storage classes in the Embedded Coder® Dictionary and Code Mappings editor. The Preserve array dimensions property that you configure for storage classes is applicable to data elements in the model. You cannot use the storage class property to preserve dimensions of bus elements because the bus objects are global types usually shared between multiple data elements such as states, signals, and so on.
To preserve dimensions of bus objects that contain multidimensional bus elements, use the Preserve element dimensions property in the Type Editor. You can preserve dimensions of bus objects for ERT models only.
Open Example Model
1. Open the model PreserveBusDims
.
PreserveBusDims;
2. Open the Embedded Coder app.
3. In the Configuration Parameters dialog box, verify that parameter Array layout is set to Row-major
.
Configure Bus Elements
The model includes these Simulink.Bus
objects in the base workspace:
ImperialSpecs
MetricSpecs
1. In the Simulink® Toolstrip, on the Modeling tab, in the Design gallery, select Type Editor.
2. In the Type Editor, set View to All
. Then, in the ImperialSpecs
row and the PreserveElementDimensions
column, select the check box. When you select this property, you configure the bus object to preserve dimensions of all elements within the bus object.
Alternatively, at the MATLAB command prompt, enter:
ImperialSpecs.PreserveElementDimensions = 1;
Generate and Review Code
Generate code for the model. On the C Code tab, click Build.
evalc('slbuild(''PreserveBusDims'')');
The generated code preserves the dimensions of the multidimensional element. In the ImperialSpecs
bus object, the element named DimensionsInInches
is a multidimensional array of [10 3] whereas the element named WeightInPounds
is a vector of [10]. The Preserve element dimensions property is applied for multidimensional arrays.
file = fullfile('PreserveBusDims_ert_rtw',... 'PreserveBusDims.h'); coder.example.extractLines(file,'#define DEFINED_TYPEDEF_FOR_ImperialSpecs_', ... '} ImperialSpecs;',0,1);
typedef struct { real_T DimensionsInInches[10][3]; real_T WeightInPounds[10]; } ImperialSpecs;
Limitations
The code generator always produces one-dimensional arrays for N-dimensional vectors, even when you specify N-dimensional indexing.