Associate model elements with code definitions
The Code Mappings editor is a graphical interface where you can configure data elements in a model, excluding referenced models, 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 tables in the Code Mappings editor display:
Data Defaults
Inports
Outports
Parameters
Data Stores
Signals/States
When you select a row in the active table, the Code section of the Property Inspector displays storage class property settings for the selected data element.
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.
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 - C 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 - C tab.
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.
Copy external code file exDblFloat.h
into a
writable folder.
copyfile(fullfile(matlabroot,'toolbox','rtw','rtwdemos','exDblFloat.h'));
Open model rtwdemo_configrpinterface
. Save a copy
of the model into the same location as file
exDblFloat.h
.
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
rtwdemo_configrpinterface.h
and
rtwdemo_configrpinterface.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 to
ExportedGlobal
. The editor updates the
default storage class setting for the two selected data element
categories.
In the Code Mappings editor, click the Inports
tab. The storage class for each inport is set to
Auto
, which means that 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 to Model 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 for
Out2
. Then, set the storage class to
Model 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 to
ImportedExtern
.
For each inport, select the row. Then, in the Property Inspector, set the Identifier property as follows:
Set In1
to
input1
.
Set In2
to
input2
.
Set In3
to
input3
.
Set In4
to
input4
.
Click Outports.
Select outport Out1
. Then, in the Property
Inspector, set the Identifier property to
output
.
Generate code and verify that the code generated for the Inport and Output blocks appears as you expect. For example:
rtwdemo_configrpinterface_private.h
includes these
declarations:
/* Exported data declaration */ /* Data with Imported storage */ extern real_T input1; /* '<Root>/In1' */ extern real_T input2; /* '<Root>/In2' */ extern real_T input3; /* '<Root>/In3' */ extern real_T input4; /* '<Root>/In4' */
rtwdemo_configrpinterface.h
lists
output
as field in output structure
ExtY_rtwdemo_configrpinterfac_T
.
/* External outputs (root outports fed by signals with default storage) */ typedef struct { real_T output; /* '<Root>/Out1' */ } ExtY_rtwdemo_configrpinterfac_T;
This code fragment shows the variable that represents
In2
, input2
, being used in the
generated entry-point step function for sample rate of 1 second.
/* Model step function for TID1 */ void rtwdemo_configrpinterface_step1(void) /* Sample time: [1.0s, 0.0s] */ { /* Lookup_n-D: '<Root>/Table1D' incorporates: * Inport: '<Root>/In2' */ rtwdemo_configrpinterface_B.Table1D = look1_binlcpw(input2, rtwdemo_configrpinterfac_ConstP.Table1D_bp01Data, rtwdemo_configrpinterfac_ConstP.Table1D_tableData, 10U); . . .
Model Element Category
— Category of model data elementsNames a category of Simulink model data elements. The storage class that you set for a category applies to elements in that category throughout the model.
Model Element Category | Description |
---|---|
Inports | Root-level input ports of a model, such as Inport and In Bus Element blocks. |
Outports | Root-level output ports of a model, such as Outport and Out Bus Element blocks. |
Signals, states, and internal data | Data elements that are internal to the model, such as block output signals, discrete block states, data stores, and zero-crossing signals. |
Shared local data stores | Data Store Memory blocks that have the block parameter Share across model instances set. These data stores are accessible only in the model where they are defined. The data store value is shared across instances of the model. |
Global data stores | Data stores that are defined by a signal object in the base workspace or in a
data dictionary. Multiple models in an application can use these data stores. To
view and configure these data stores in the Code Mappings editor, click the
Refresh link to the right of the category name. Clicking
this link updates the model diagram. |
Model parameters | Parameters that are defined within a model, such as parameters in the model workspace. Excludes model arguments. |
External parameters | Parameters that you define as objects in the base workspace or in a data dictionary. Multiple
models in an application can use these parameters. To view and configure these
parameters in the Code Mappings editor, click the Refresh
link to the right of the category name. Clicking this link updates the model
diagram. |
The Code Mappings editor presents valid storage class options for a given category. The options can include:
Unspecified storage class (Default
).
The code generator places the code for the category of data elements
in standard structures, such as B_
,
ExtY_
, ExtU_
,
DW_
, and P_
. See Standard Data Structures in the Generated Code.
Relevant predefined storage classes, such as
ExportedGlobal
.
Relevant storage classes in an available package, such as
ImportFromFile
(requires Embedded Coder®) .
Storage class defined in an Embedded Coder Dictionary (requires Embedded Coder) .
Storage Class
— Code definition for model data elementsDefinition (specification) that the code generator uses to determine
properties, such as appearance and location, for code that it produces for
model data elements. Valid settings are
Default
,
ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See
Choose Storage Class for Controlling Data Representation in Generated Code.
Source
— Name of root-level Inport block or bus elementIdentifies a root Inport block or an element of an In Bus Element block
(for example, InBus1.signal1
) in the model. If the
element resolves to a data object, the Code Mappings editor displays a
resolve-to-signal-object icon to the right of the source name and resolves
the configuration based on whether the storage class setting for the element
is Auto
. If the storage class is Auto
,
the data element assumes the code configuration that the data object
specifies. The editor changes the display text in the Storage
Class column to From signal object:
followed by the name of the storage class of the data object. If the storage
class is not Auto
, the data element assumes the
configuration that you specify in the Code Mappings editor.
Storage Class
— Code definition for root inportDefinition that the code generator uses to determine properties, such as
appearance and location, for code that it produces for the root inport.
Valid settings are
Auto
, Model
default
, ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See Choose Storage Class for Controlling Data Representation in Generated Code.
Source
— Name of root Outport block or bus elementIdentifies a root-level Outport block or an element of an
Out Bus Element block (for example,
OutBus1.signal1
) in the model. If the element
resolves to a data object, the Code Mappings editor displays a
resolve-to-signal-object icon to the right of the source name and resolves
the configuration based on whether the storage class setting for the element
is Auto
. If the storage class is Auto
,
the data element assumes the code configuration that the data object
specifies. The editor changes the display text in the Storage
Class column to From signal object:
followed by the name of the storage class of the data object. If the storage
class is not Auto
, the data element assumes the
configuration that you specify in the Code Mappings editor.
Storage Class
— Code definition for root outportDefinition that the code generator uses to determine properties, such as
appearance and location, for code that it produces for the root outport.
Valid settings are
Auto
, Model
default
, ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See
Choose Storage Class for Controlling Data Representation in Generated Code.
Source
— Name of model parameter argument, model parameter, or external parameterIdentifies a parameter in the model. If the element resolves to a data
object, the Code Mappings editor displays a resolve-to-parameter-object icon
to the right of the source name and resolves the configuration based on
whether the storage class setting for the element is
Auto
. If the storage class is Auto
,
the data element assumes the code configuration that the data object
specifies. The editor changes the display text in the Storage
Class column to From parameter object:
followed by the name of the storage class of the data object. If the storage
class is not Auto
, the data element assumes the
configuration that you specify in the code mappings.
Types of parameter elements are listed in this table.
Type of Parameter Element | Description |
---|---|
Model parameter | Parameter that is defined within a model, such as a parameter in the model workspace. Excludes model arguments. |
External parameter | Parameter that you define as an object in the base
workspace or in a data dictionary. Multiple models in an
application can use these parameters. This grouping of
parameters appears in the editor only if the model uses such
an element. To view and configure these parameters in the
Code Mappings editor, click the Refresh
link to the right of the category name. Clicking this link
updates the model diagram. |
Storage Class
— Code definition for parameterDefinition that the code generator uses to determine properties, such as
appearance and location, for code that it produces for the parameter. For
external parameters, after you click the Refresh
link to
the right of the category name, the compiled storage class (for example, the
storage class configured for an external parameter) appears on the right
side of the Storage Class column. Valid settings are Auto
,
Model default
,
ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See
Choose Storage Class for Controlling Data Representation in Generated Code.
Source
— Name of local data store, shared local data store, or global data storeIdentifies a data store in the model. If the element resolves to a data
object, the Code Mappings editor displays a resolve-to-signal-object icon to
the right of the source name and resolves the configuration based on whether
the storage class setting for the element is Auto
. If the
storage class is Auto
, the data element assumes the code
configuration that the data object specifies. The editor changes the display
text in the Storage Class column to From
signal object:
followed by the name of the storage class of
the data object. If the storage class is not Auto
, the
data element assumes the configuration that you specify in the code
mappings.
Types of data store elements are listed in this table.
Type of Data Store Element | Description |
---|---|
Local data store | Data store that is accessible from anywhere in the model hierarchy that is at or below the level at which you define the data store. You can define a local data store graphically in a model by including a Data Store Memory block or by creating a signal object (synthesized data store) in the model workspace. |
Shared local data store | Data Store Memory block that has the block parameter Share across model instances set. These data stores are accessible only in the model where they are defined. The data store value is shared across instances of the model. This grouping of data stores appears in the editor only if such an element exists in the model. |
Global data store | Data store that is defined by a signal object in the base
workspace or in a data dictionary. Multiple models in an
application can use these data stores. These data stores are
not configurable in the code mappings. After you click the
refresh button, they appear in the Code Mappings editor in a
read-only state for viewing or accounting purposes. This
grouping of data stores appears in the editor only if the
model uses such an element. To view and configure these data
stores in the Code Mappings editor, click the
Refresh link to the right of the
category name. Clicking this link updates the model
diagram. |
Names of local and shared local data stores appear in the format
. block-name
:
data-store-name
Depending on how the data store element is represented and configured in the model, local and shared local data stores can resolve to a signal object in the model workspace, based workspace, or a data dictionary. Global data stores resolve to a signal object in the base workspace or a data dictionary.
Storage Class
— Code definition for data storeDefinition that the code generator uses to determine properties, such as
appearance and location, for code that it produces for the data store. For
global data stores, after you click the Refresh
link to
the right of the category name, the compiled storage class (for example, the
storage class configured for a global data store) appears on the right side
of the Storage Class column. Valid settings are Auto
, Model
default
, ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See
Choose Storage Class for Controlling Data Representation in Generated Code.
Path
— Path to data store in modelLink that you can click to highlight the data store in the model diagram.
Source
— Name of signal or stateIdentifies a signal line or state in the model. If the element resolves to
a data object, the Code Mappings editor displays a resolve -to-signal-object
icon to the right of the source name and resolves the configuration based on
whether the storage class setting for the element is
Auto
. If the storage class is Auto
,
the data element assumes the code configuration that the data object
specifies. The editor changes the display text in the Storage
Class column to From signal object:
followed by the name of the storage class of the data object. If the storage
class is not Auto
, the data element assumes the
configuration that you specify in the Code Mappings editor.
The Code Mappings editor lists:
Named signals and states by using the data element name
Unnamed signals by using the format
source-block
:
port-number
States used in multiple blocks by using the format
block-name
:
state-name
To configure an individual signal line in the Code Mappings editor for a model, first you must add the signal to the mappings. See Configure Signal Data for C Code Generation.
Storage Class
— Code definition for signalDefinition that the code generator uses to determine properties, such as
appearance and location, for code that it produces for the signal line or
state. Valid settings are
Auto
, Model
default
, ExportedGlobal
,
ImportedExtern
, and
ImportedExternPointer
. See
Choose Storage Class for Controlling Data Representation in Generated Code.
Path
— Path to signal line or state in modelLink that you can click to highlight the signal line or block that uses the state in the model diagram.