Main Content

Configure Data Interface

Embedded Coder® reduces the effort for configuring data and function interfaces by providing a way to specify default configurations for categories of data elements and functions across a model. Applying default configurations can save time and reduce the risk of introducing errors in code, especially for larger models and models from which you generate multi-instance code. After applying default configurations, you can selectively override the default settings for individual data elements and functions.

Customize the data interface of model rtwdemo_roll by configuring function roll_control_step to:

  • Read input data from global variables that are declared and defined in external files roll_input_data.h and roll_input_data.c.

  • Write output data to global variables that the code generator declares in output_data.h and defines in output_data.c.

To make these changes, in the MATLAB® Command Window, copy these external code files to your current MATLAB working folder.


The data interface configuration changes that you make depend on these files being accessible for code generation and the build process. The build process compiles the generated code with the code that is in these files.

Configure Default Code Generation for Data

Configure default code generation configurations for model inports and outports.

  1. In the C Code tab, select Code Interface > Default Code Mappings.

  2. Configure Inport blocks at the root level of the model to appear in the generated code as separate global variables defined by external code. In the Code Mappings editor, under Inports and Outports, select category Inports. Set the default storage class to ImportFromFile.

    With this setting, the generated code does not define global variables that represent the inport data. Instead, a #include statement includes a header file that declares the input variables. You specify the name of the header file with the Property Inspector.

  3. In the Property Inspector, set property HeaderFile to roll_input_data.h.

  4. To see how the extern declarations in external header file roll_input_data.h name the input variables, in the MATLAB Command Window, open roll_input_data.h located in your current working folder.

    extern boolean_T AP_Eng;
    extern real32_T HDG_Ref;
    extern real32_T Rate_FB;
    extern real32_T Phi;
    extern real32_T Psi;
    extern real32_T TAS;
    extern real32_T Turn_Knob;
  5. Configure the code generation naming rule for global variables. By default, the code generator names global variables with the prefix rt. For the code generator to produce code that matches the external variable declarations in roll_input_data.h, configure the code generation naming rule for global variables accordingly.

    1. Open the Model Configuration Parameters dialog box. In the toolstrip, on the C Code tab, click Settings.

    2. Navigate to the Code Generation > Identifiers pane.

    3. Set parameter Global variables to the naming rule $N$M (remove the rt prefix). Token $N represents the name of a data element in the model, for example, the name of an Inport or Outport block. Token $M represents name-mangling text that the code generator inserts, if necessary, to avoid name collisions with other global variables in the code.

    4. Apply the change.

  6. Configure Outport blocks at the root level of the model to appear in the generated code as separate global variables. In the Code Mappings editor, on the Data Defaults tab, for category Outports, set Storage Class to ExportToFile.

    The generated code declares and defines the output variables in header and definition files that you specify with the Property Inspector.

  7. In the Property Inspector, specify the names for the generated header and definition files. Set property HeaderFile to roll_output_data.h and property DefinitionFile to roll_output_data.c.

  8. Configure code generation for the model to include the external source files roll_input_data.c and roll_heading_mode.c. In the Configuration Parameters dialog box, set Code Generation > Custom Code > Additional build information > Source files to roll_input_data.c roll_heading_mode.c. Then, click Apply and OK.

  9. Save the model. Regenerate the code by clicking Build.

    A compiler error indicates that variable HDG_Mode is not declared. That variable is not declared in header file roll_input_data.h, which you declared as the default header file for inports. You fix this error in the next section of this tutorial.

    The model is configured to open the code generation report after code generation is complete. Minimize this report window for exploration later in this tutorial.

  10. You configured Inport blocks to use an external header file to declare and define input variables. In the Code view, confirm that the generated code includes this external header file by searching for roll_input_data.h.

  11. Search for the root level Inport block name, HDG_Ref. As you type, choose the search suggestion with the green V icon. This search suggestion finds instances of HDG_Ref used as a variable in the generated code. Confirm that HDG_Ref is defined as a separate global variable.

  12. In the model, rtwdemo_roll, click the Outport block Ail_Cmd. Place your cursor over the ellipsis menu above the block and click Navigate To Code. The Code view highlights code in rtwdemo_roll.c that corresponds to the block. In the code, place your cursor over the ellipsis menu above the output variable Ail_Cmd. The traceability dialog box displays variable definitions and model elements that correspond to the code. The dialog box confirms that Ail_Cmd is defined as a separate global variable. Click the definition code to see the definition in output_data.c.

Override Default Settings for Individual Data Elements

The settings that you choose for a category under Data Defaults apply to elements in that category across a model. To override the default settings for an individual data element, use the Code Mappings editor.

When you generated code after configuring default settings for inports and outports, a compiler error indicated that variable HDG_Mode is not declared. You can fix that error by overriding the default configuration for Inport block HDG_Mode.

  1. In the Code Mappings editor, on the Inports tab, select source HDG_Mode.

  2. Set Storage Class to ImportFromFile.

  3. In the Property Inspector, set Identifier to HDG_Mode and Header File to roll_heading_mode.h.

    Based on these settings, the code generator imports the declaration for external variable HDG_Mode from header file roll_heading_mode.h.

    extern boolean_T HDG_Mode;
  4. Save the model and regenerate the code.

    Minimize the code generation report window for exploration later in this tutorial.

  5. In the Code view, search for roll_heading_mode.h and confirm that it is included in the generated code with the default configuration file roll_input_data.h.

  6. Search for HDG_Mode and confirm that it is defined as a separate global variable.

Next, configure a model parameter to be a global variable in the generated code. As a global variable, you can tune the parameter value at run time.