| Contents | Index |
| On this page… |
|---|
Enable Data Objects for Generated Code |
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.mdl
Most programming languages require that you declare data before using it. The declaration specifies the following information:
| Data Attribute | Description |
|---|---|
| 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
| Name | Description |
|---|---|
| double | Double-precision floating point |
| single | Single-precision floating point |
| int8 | Signed 8-bit integer |
| uint8 | Unsigned 8-bit integer |
| int16 | Signed 16-bit integer |
| uint16 | Unsigned 16-bit integer |
| int32 | Signed 32-bit integer |
| uint32 | 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
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 a variety of ways. This example focuses on the following types of data objects:
Signal
Parameter
Bus
To configure the data interface for your model using the data object method, in the MATLAB base workspace, you define data objects and 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.
A data object has a mixture of active and descriptive fields. Active fields affect simulation or code generation. Descriptive fields do not affect simulation or code generation. They are used with data dictionaries and model-checking tools.
Active fields:
Data type
Storage class
Value (parameters)
Initial value (signals)
Alias (define a different name in the generated code)
Dimension (inherited for parameters)
Complexity (inherited for parameters)
Descriptive fields:
Minimum
Maximum
Units
Description
You can create and inspect base workspace data objects by entering commands in the MATLAB Command Window or by using Model Explorer. Perform the following steps to explore base workspace signal data objects.
Open rtwdemo_throttlecntrl_datainterface.mdl and save a copy as throttlecntrl_datainterface.mdl in a writable location on your MATLAB path.
Open Model Explorer.
Select Base Workspace.
Select the 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:
pos_cmd_one =
Simulink.Signal (handle)
CoderInfo: [1x1 Simulink.SignalCoderInfo]
Description: [1x54 char]
DataType: 'double'
Min: -1
Max: 1
DocUnits: 'Norm'
Dimensions: -1
DimensionsMode: 'auto'
Complexity: 'auto'
SampleTime: -1
SamplingMode: 'auto'
InitialValue: '0'To view other signal objects, in Model Explorer, click the object name or in the MATLAB Command Window, enter the object name. The following table summarizes object characteristics for some of the data objects in this model.
| Object Characteristics | pos_cmd_one | pos_rqst | P_InErrMap | ThrotComm* | ThrottleCommands* |
|---|---|---|---|---|---|
| Description | Top-level output | Top-level input | Calibration parameter | Top-level output structure | Bus definition |
| Data type | Double | Double | Auto | Auto | Structure |
| 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 below.

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: fbk_1 and pos_cmd_two.
To find the signals and create data objects for them:
In the model window, select Tools > Data Object Wizard. The Data Object Wizard dialog box opens.
To find candidate constructs, click Find. Constructs fbk_1 and pos_cmd_two appear in the dialog box.
To select both constructs, click Check All.
To apply the default Simulink package for the data objects, click Apply Package.
To create the data objects, click Create. Constructs fbk_1 and pos_cmd_two are removed from the dialog box.
Close the Data Object Wizard.
In the Contents pane of the Model Explorer, find the newly created objects fbk_1 and pos_cmd_two.
In the Model Explorer Model Hierarchy, expand the throttlecntrl_datainterface model node.
Click the Configuration (Active) node. Make sure that you select, Optimization > Signals and Parameters > Inline parameters.
Enable a signal to appear in generated code.
In the model window, right-click the pos_cmd_one signal line and select Signal Properties. A Signal Properties dialog box opens.
Make sure that you select the Signal name must resolve to Simulink signal object parameter.

Enable signal object resolution for all signals in the model simultaneously. In the MATLAB Command Window, enter:
disableimplicitsignalresolution('throttlecntrl_datainterface')Save and close throttlecntrl_datainterface.mdl.
In the throttle controller model, all data types are set to double. 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 by running the test harness.
Before you run your test harness, update it to include the throttlecntrl_datainterface model.
Open throttlecntrl_datainterface.mdl.
Open your copy of test harness,throttlecntrl_testharness.mdl.
Right-click the Unit_Under_Test Model block and select Model Reference Parameters.
Set Model name (without the .mdl extension) to throttlecntrl_datainterface. Click OK.
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 throttlecntrl_testharness.mdl.
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 save.
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 targets)
Multiple models, one data set:
Sharing data between models in a system
Sharing data between projects (for example, transmission, engine, and wheel controllers might all 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, you 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.
You must explicitly configure data objects to appear by name in generated code.
Because Simulink software uses the double data type for simulation, if all data types are set to double for a model, expect simulation and generated code behavior to match.
Separation of data from model provides several benefits.
Working with Data in the Simulink documentation
Custom Storage Classes in the Embedded Coder documentation
Manage Placement of Data Definitions and Declarations in the Embedded Coder documentation
![]() | Configure the Model and Generating Code | Call External C Functions from a Model and Generated Code | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |