Skip to Main Content Skip to Search
Product Documentation

Configure the Data Interface

About This Example

Learning Objectives

Prerequisites

Required File

rtwdemo_throttlecntrl_datainterface.mdl

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

NameDescription
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

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 a variety of 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 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.

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.

  1. Open rtwdemo_throttlecntrl_datainterface.mdl and save a copy as throttlecntrl_datainterface.mdl in a writable location on your MATLAB path.

  2. Open Model Explorer.

  3. Select Base Workspace.

  4. 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'
  5. 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 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 below.

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 model window, select Tools > 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 Check All.

  4. To apply the default Simulink package for the data objects, click Apply Package.

  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. In the Model Explorer Model Hierarchy, expand the throttlecntrl_datainterface model node.

  2. Click the Configuration (Active) node. Make sure that you select, Optimization > Signals and Parameters > Inline parameters.

  3. Enable a signal to appear in generated code.

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

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

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

    disableimplicitsignalresolution('throttlecntrl_datainterface')
  5. Save and close throttlecntrl_datainterface.mdl.

Get Familiar with Effects of Simulation on Data Typing

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.

  1. Open throttlecntrl_datainterface.mdl.

  2. Open your copy of test harness,throttlecntrl_testharness.mdl.

  3. Right-click the Unit_Under_Test Model block and select Model Reference Parameters.

  4. Set Model name (without the .mdl extension) to throttlecntrl_datainterface. Click OK.

  5. Update the test harness model diagram.

  6. Simulate the test harness.

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

  7. Save and close throttlecntrl_testharness.mdl.

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:

Key Points

Learn More

  


Related Products & Applications

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