Main Content

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 Bus Editor. You can preserve dimensions of bus objects for ERT models only.

Open Example Model

1. Open the model rtwdemo_preservedimensions_slbus.


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

1. The model includes these Simulink.Bus objects in the base workspace:

  • ImperialSpecs

  • MetricSpecs

On the Simulink Toolstrip, the Modeling tab, in the Design gallery, click Bus Editor. Alternatively, at the MATLAB command prompt, enter buseditor.

2. For the bus object ImperialSpecs, open the Code Generation tab and select Preserve element dimensions checkbox.

Alternatively, at the MATLAB command prompt, enter:

ImperialSpecs.PreserveElementDimensions = 1;

3. Click Apply and close the Bus Editor dialog box.

Generate and Review Code

Generate code for the model. On the C Code tab, click Build.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

The generated code preserves the dimensions of the multidimensional bus element. In the ImperialSpecs bus object, the bus element DimensionsInInches is a multidimensional array of [10 3] whereas the bus element WeightInPounds is a vector of [10]. The Preserve element dimensions property is applied for multidimensional arrays.

file = fullfile('rtwdemo_preservedimensions_slbus_ert_rtw',...
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_ImperialSpecs_', ...
    '} ImperialSpecs;',0,1);
typedef struct {
  real_T DimensionsInInches[10][3];
  real_T WeightInPounds[10];
} ImperialSpecs;

Related Topics