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

Open Example Model

1. Open the model rtwdemo_preservedimensions_slbus.

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

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, click Type Editor. Alternatively, at the MATLAB® command prompt, enter typeeditor.

2. Select the bus object named ImperialSpecs. In the Property Inspector pane, expand the Code Generation section and select Preserve element dimensions. 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;

3. Close the Type Editor.

Generate and Review Code

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

currentDir = pwd;
[~,cgDir] = rtwdemodir();
evalc('slbuild(''rtwdemo_preservedimensions_slbus'')');

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('rtwdemo_preservedimensions_slbus_ert_rtw',...
    'rtwdemo_preservedimensions_slbus.h');
rtwdemodbtype(file,'#define DEFINED_TYPEDEF_FOR_ImperialSpecs_', ...
    '} ImperialSpecs;',0,1);
typedef struct {
  real_T DimensionsInInches[10][3];
  real_T WeightInPounds[10];
} ImperialSpecs;

Related Topics