Configure the data interface for code generated for a model.
Control the name, data type, and data storage class of signals and parameters in generated code.
Understanding ways to represent and use data and signals in models.
Familiarity with representing data constructs as data objects.
Ability to read C code.
rtwdemo_throttlecntrl_datainterface model file
Most programming languages require that you declare data before using it. The declaration specifies the following information:
|Scope||The region of the program that has access to the data|
|Duration||The period during which the data is resident in memory|
|Data type||The amount of memory allocated for the data|
|Initialization||An initial value, a pointer to memory, or NULL. If you do not provide an initial value, most compilers assign a zero value or a null pointer.|
The following data types are supported for code generation.
Supported Data Types
|Double-precision floating point|
|Single-precision floating point|
|Signed 8-bit integer|
|Unsigned 8-bit integer|
|Signed 16-bit integer|
|Unsigned 16-bit integer|
|Signed 32-bit integer|
|Unsigned 32-bit integer|
|Fixed-point data types||8-, 16-, 32-bit word lengths|
A storage class is the scope and duration of a data item. For more information about storage classes, see C Code Generation Configuration for Model Interface Elements.
In Simulink® models and Stateflow® charts, the following methods are available for declaring data: data objects and direct specification. This example uses the data object method. Both methods allow full control over the data type and storage class. You can mix the two methods in a single model.
In the MATLAB® and Simulink environment, you can use data objects in various ways. This example focuses on the following types of data objects:
To configure the data interface for your model using the data object method, in the MATLAB base workspace, you define data objects. Then, associate them with your Simulink model or embedded Stateflow chart. When you build your model, the build process uses the associated base workspace data objects in the generated code.
You can set the values of the data object properties, which include:
Initial value (signals)
Identifier (define a different name in the generated code)
Dimension (typically inherited for parameters)
Complexity (inherited for parameters)
Unit (physical measurement unit)
Description (used to document your data objects — does not affect simulation or code generation)
You can create and inspect base workspace data objects by entering commands in the MATLAB Command Window or by using Model Explorer. To explore base workspace signal data objects, use these steps:
rtwdemo_throttlecntrl_datainterface and save a copy as
throttlecntrl_datainterface in a writable location on your MATLAB
Open Model Explorer.
Select Base Workspace.
pos_cmd_one signal object for viewing.
You can also view the definition of a signal object. In the MATLAB Command Window, enter
pos_cmd_one = Signal with properties: CoderInfo: [1x1 Simulink.CoderInfo] Description: 'Throttle position command from the first PI controller' DataType: 'double' Min: -1 Max: 1 Unit: '' Dimensions: -1 DimensionsMode: 'auto' Complexity: 'auto' SampleTime: -1 InitialValue: '0'
To view other signal objects, in Model Explorer, click the object name or in the MATLAB Command Window, enter the object name. This table summarizes object characteristics for some of the data objects in this model.
|Description||Top-level output||Top-level input||Calibration parameter||Top-level output structure||Bus definition|
|Storage class||Exported global||Imported extern pointer||Constant||Exported global||None|
ThrottleCommands defines a Bus object;
ThrotComm is an instantiation of the bus. If the bus is a
nonvirtual bus, the signal generates a structure in the C code.
You can use a bus definition (
ThrottleCommands) to instantiate
multiple instances of the structure. In a model diagram, a bus object appears as a wide line
with central dashes, as shown.
You can create data objects for named signals, states, and parameters. To associate a data object with a construct, the construct must have a name.
To find constructs for which you can create data objects, use the Data Object Wizard.
This tool finds the constructs and then creates the objects for you. The model includes two
signals that are not associated with data objects:
To find the signals and create data objects for them:
In the Simulink Editor, in the Modeling tab, under Design, click Data Object Wizard. The Data Object Wizard dialog box opens.
To find candidate constructs, click Find. Constructs
pos_cmd_two appear in the dialog
To select both constructs, click Select All.
In the table, under Class, make sure that each proposed data
object uses the class
Simulink.Signal. To change the class of the
objects, click Change Class.
To create the data objects, click Create. Constructs
pos_cmd_two are removed from the
Close the Data Object Wizard.
In the Contents pane of the Model Explorer, find the newly
Enable a signal to appear in generated code.
In the Simulink Editor, right-click the
line and select Properties. A Signal Properties dialog box
Make sure that you select the Signal name must resolve to Simulink signal object parameter.
Enable signal object resolution for the signals in the model. In the MATLAB Command Window, enter:
Save and close
In the throttle controller model, the data types are set to
Because Simulink software uses the
double data type for simulation, do not
expect changes in the model behavior when you run the generated code. You verify this
effect by running the test harness.
Before you run your test harness, update it to include the
The following procedure requires a Stateflow license.
Open your copy of test harness,
Model block and select Block Parameters
Set Model name to
Update the test harness model diagram.
Simulate the test harness.
The resulting plot shows that the difference between the golden and simulated versions of the model remains zero.
Save and close
Data objects exist in a separate file from the model in the base workspace. To save the
data manually, in the MATLAB Command Window, enter
The separation of data from the model provides the following benefits:
One model, multiple data sets:
Use of different parameter values to change the behavior of the control algorithm (for example, for reusable components with different calibration values)
Use of different data types to change targeted hardware (for example, for floating-point and fixed-point targeted hardware)
Multiple models, one data set:
Sharing data between models in a system
Sharing data between projects (for example, transmission, engine, and wheel controllers can use the same CAN message data set)
You can declare data in Simulink models and Stateflow charts by using data objects or direct specification.
From the Model Explorer or from the command line in the MATLAB Command Window, manage (create, view, configure, and so on) base workspace data.
The Data Object Wizard provides a quick way to create data objects for constructs such as signals, buses, and parameters.
Configure data objects explicitly to appear by name in generated code.
Separation of data from model provides several benefits.