Code Mappings Editor – C
Description
The Code Mappings editor is a graphical interface where you can configure data elements in a model for code generation. Each model in a model reference hierarchy has its own code mappings. Associate each category of model data element with a specific storage class throughout a model. Then, override those settings, as needed, for specific data elements.
A storage class defines properties such as appearance and location, which the code generator uses when producing code for associated data.
To configure data elements and functions for code generation, use the tabs in the Code Mappings editor display:
Data Defaults
Inports
Outports
Parameters
Data Stores
Signals/States
When there are additional code mapping properties that can be configured for a model element, you can configure these properties by selecting a row in the active table and clicking the icon.
Before you can configure a signal for code generation, add the signal to the model code mappings. Add and remove signals from the code mappings by pausing on the ellipsis that appears above or below a signal line to open the action bar. Click the Add Signal or Remove Signal button. These buttons are also available in the Code Mappings editor on the Signals/States tab.
Open the Code Mappings Editor – C
Do one of the following:
Open the Simulink® Coder™ app. On the C Code tab, select Code Interface > Default Code Mappings or Code Interface > Individual Element Code Mappings.
Open the Simulink Coder app. On the C Code tab, in the bottom left corner of the Simulink Editor window, click the Code Mappings tab.
In the model canvas of the Simulink Editor window, click the perspective control in the lower-right corner and select Code. Then, click the Code Mappings tab.
Examples
Configure Code Generation for Root-Level Inport and Outport Blocks
Configure code generation for the root-level Inport and Outport blocks throughout a model. Applying default configurations can save time, especially for large-scale models that use a significant amount of data. After applying default mappings, you can adjust mappings for individual data elements.
Open the model
ConfigurationRapidPrototypingInterface
by entering this command at the MATLAB® Command Window:openExample("ConfigurationRapidPrototypingInterface")
Open the Simulink Coder app. The C Code tab includes the Code Mappings editor.
Configure the code generator to declare and define global variables for
inports and outports in generated files
ConfigurationRapidPrototypingInterface.h
and
ConfigurationRapidPrototypingInterface.c
.
In the C Code tab, select Code Interface > Default Code Mappings.
In the Data Defaults tab, under Inports and Outports, select the row for Inports. Then, set the storage class to
ImportedExternPointer
. Set the storage class for Outports toExportedGlobal
. The editor updates the default storage class setting for the two selected data element categories.
In the Code Mappings editor, click the Inports tab. When the storage class is set to
Auto
, the code generator might eliminate or change the representation of relevant code for optimization purposes. If optimizations are not possible, the code generator applies the model default configuration.Force the code generator to use the default configuration for inports, which is storage class
ImportedExternPointer
. Press the Ctrl key and select the inports. For one of the selected inports, set the storage class toModel default: ImportedExternPointer
. The editor updates the storage class setting for the selected inports.Force the code generator to use storage class
ExportedGlobal
for the model root outport. Click the Outports tab. Select the row forOut2
. Then, set the storage class toModel default: ExportedGlobal
.
To configure properties for individual data elements, for example, if you need
to override default configuration settings, use the tabs for the different data
element types. For this example, override the default storage class setting for
Inport block In1
.
By default, the code generator names inport and outport variables based on the
Inport or Outport block name in the model.
When you configure data elements with a storage class setting other than
Auto
, you can override that default setting for
individual elements by setting storage class property
Identifier. This property enables you to specify an
identifier for the code without modifying the model design.
For this example, set Identifier for the Inport and Outport blocks.
In the Code Mappings editor, click the Inports tab.
For
In1
, set the storage class toImportedExtern
.For each inport, select the row. Then, click the icon. Set the Identifier property as follows:
Set
In1
toinput1
.Set
In2
toinput2
.Set
In3
toinput3
.Set
In4
toinput4
.
Click Outports.
Select outport
Out1
. Click the icon and set the Identifier property tooutput
.
Generate code and verify that the code generated for the Inport and Output blocks appears as you expect. For example:
ConfigurationRapidPrototypingInterface_private.h
includes these declarations:/* Imported (extern) block signals */ extern real_T input1; /* '<Root>/In1' */ /* Exported data declaration */ /* Data with Imported storage (pointer) */ extern real_T *input2; /* '<Root>/In2' */ extern real_T *input3; /* '<Root>/In3' */ extern real_T *input4; /* '<Root>/In4' */
ConfigurationRapidPrototypingInterface.h
includes these declarations./* Data with Exported storage */ extern real_T output; /* '<Root>/Out1' */
This code fragment shows the variable that represents
In1
,input1
, being used in the generated entry-point step function./* Model step function */ void ConfigurationRapidPrototypingInterface_step(void) { /* DataStoreWrite: '<Root>/Data Store Write' incorporates: * Constant: '<Root>/Constant1' * Constant: '<Root>/Constant2' * Inport: '<Root>/In1' * Logic: '<Root>/LogOp' * RelationalOperator: '<Root>/RelOp1' * RelationalOperator: '<Root>/RelOp2' */ mode = ((input1 > 10.0) || (input1 < -10.0)); . . .
Related Examples
- C Data Code Interface Configuration for Model Interface Elements
- Choose Data Configuration Approach
- Choose Storage Class for Controlling Data Representation in Generated Code
- Configure Generated C Function Interface for Model Entry-Point Functions
Parameters
Version History
Introduced in R2020b