Main Content

Configure Data Interface

About This Example

Learning Objectives

  • 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.

Required Files

Model files ThrottleControlDataInterface and ThrottleControlTestHarness

Declare Data

Most programming languages require that you declare data before using it. The declaration specifies the following information:

Data AttributeDescription
ScopeThe region of the program that has access to the data
DurationThe period during which the data is resident in memory
Data typeThe amount of memory allocated for the data
InitializationAn 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

doubleDouble-precision floating point
singleSingle-precision floating point
int8Signed 8-bit integer
uint8Unsigned 8-bit integer
int16Signed 16-bit integer
uint16Unsigned 16-bit integer
int32Signed 32-bit integer
uint32Unsigned 32-bit integer
Fixed-point data types8-, 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 Data Code Interface Configuration for Model Interface Elements.

Use Data Objects

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:

  • 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. 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:

  • Data type

  • Storage class

  • Value (parameters)

  • 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)

  • Minimum value

  • Maximum value

  • Description (used to document your data objects — does not affect simulation or code generation)

Open Example Model

Open the example model ThrottleControlDataInterface.


Explore Base Workspace Signal Data Objects

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:

  1. Open Model Explorer.

  2. Select Base Workspace.

  3. 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 =
    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'
  4. 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.

    Object Characteristicspos_cmd_onepos_rqstP_InErrMapThrotComm*ThrottleCommands*
    DescriptionTop-level outputTop-level inputCalibration parameterTop-level output structureBus definition
    Data typeDoubleDoubleAutoAutoStructure
    Storage classExported globalImported extern pointerConstantExported globalNone

    * 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.

Add New Data Objects

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:

  1. In the Simulink Editor, in the Modeling tab, under Design, click Data Object Wizard. The Data Object Wizard dialog box opens.

  2. To find candidate constructs, click Find. Constructs fbk_1 and pos_cmd_two appear in the dialog box.

  3. To select both constructs, click Select All.

  4. 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.

  5. To create the data objects, click Create. Constructs fbk_1 and pos_cmd_two are removed from the dialog box.

  6. Close the Data Object Wizard.

  7. In the Contents pane of the Model Explorer, find the newly created objects fbk_1 and pos_cmd_two.

Enable Data Objects for Generated Code

  1. Enable a signal to appear in generated code.

    1. In the Simulink Editor, right-click the pos_cmd_one signal line and select Properties. A Signal Properties dialog box opens.

    2. Make sure that you select the Signal name must resolve to Simulink signal object parameter.

  2. Enable signal object resolution for the signals in the model. In the MATLAB Command Window, enter:


Effects of Simulation on Data Typing

In the throttle controller model, the 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 effect by running the test harness.

Before you run your test harness, update it to include the ThrottleControlDataInterface model.


The following procedure requires a Stateflow license.

  1. Open your copy of test harness, ThrottleControlTestHarness.

  2. Right-click the Unit_Under_Test Model block and select Block Parameters (ModelReference).

  3. Set Model name to ThrottleControlDataInterface. Click OK.

  4. Update the test harness model diagram.

  5. Simulate the test harness.

    The resulting plot shows that the difference between the golden and simulated versions of the model remains zero.

Manage Data

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 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)

Key Points

  • 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.

Related Topics