Main Content

Configure Model and Generate Code

About This Example

Learning Objectives

  • Configure a model for code generation.

  • Apply model checking tools to discover conditions and configuration settings resulting in generation of inaccurate or inefficient code.

  • Generate code from a model.

  • Locate and identify generated code files.

  • Review generated code.


  • Ability to open and modify Simulink® models and subsystems.

  • Ability to set model configuration parameters.

  • Ability to use the Simulink Model Advisor.

  • Ability to read C code.

  • An installed, supported C compiler.

Required Files

rtwdemo_throttlecntrl model file

Configure the Model for Code Generation

Model configuration parameters determine the method for generating the code and the resulting format.

  1. Open rtwdemo_throttlecntrl and save a copy as throttlecntrl in a writable location on your MATLAB path. Then, open the Simulink Coder app.

  2. Open the Configuration Parameters dialog box, Solver pane. To generate code for a model, you must configure the model to use a fixed-step solver. The following table shows the solver configuration for this example.

    ParameterSettingEffect on Generated Code
    TypeFixed-stepMaintains a constant (fixed) step size, which is required for code generation
    Solverdiscrete (no continuous states)Applies a fixed-step integration technique for computing the state derivative of the model
    Fixed-step size.001Sets the base rate; must be the lowest common multiple of the rates in the system

  3. Set model configuration parameter System target file to grt.tlc.


    The GRT (Generic Real-Time Target) configuration requires a fixed-step solver. However, the rsim.tlc system target file supports variable step code generation.

    The system target file defines an environment for generating and building code for execution on a certain hardware or operating system platform. For example, one property of a system target file is the value for the CodeFormat TLC variable. The GRT configuration requires a fixed step solver and the rsim.tlc supports variable step code generation.

  4. Set model configuration parameter Include directories as follows:


    This folder includes files that are required to build an executable for the model.

  5. Close the Model Configuration Parameters dialog box.

Save Your Model Configuration as a MATLAB Function

You can save the settings of model configuration parameters as a MATLAB® function by using the getActiveConfigSet function. In the MATLAB Command Window, enter:

thcntrlAcs = getActiveConfigSet('throttlecntrl');

You can then use the resulting function (for example, throttlecntrlModelConfig) to:

  • Archive the model configuration.

  • Compare different model configurations by using differencing tools.

  • Set the configuration of other models.

For example, you can set the configuration of model myModel to match the configuration of the throttle controller model by opening myModel and entering:

myModelAcs = throttlecntrlModelConfig;
attachConfigSet('myModel', myModelAcs, true);
setActiveConfigSet('myModel', myModelAcs.Name);

For more information, see Save a Configuration Set and Load a Saved Configuration Set.

Check Model Conditions and Configuration Settings

Before generating code for a model, use the Simulink Model Advisor to check the model for conditions and configuration settings. This check finds issues that can result in inaccurate or inefficient code.

  1. Open model throttlecntrl. Then, open the Simulink Coder app.

  2. Start the Model Advisor. In the C Code tab, click C/C++ Code Advisor. A dialog box opens showing the model system hierarchy.

  3. Click rtwdemo_throttlecntrl and then click OK. The Model Advisor window opens.

  4. In the right pane, select Run this check. The report shows a Run Summary that flags check warnings.

  5. Review the report. The warnings highlight issues. At this point, you can ignore them. For more information about reports, see View Model Advisor Check Reports.

Generate Code for the Model

  1. Open throttlecntrl. Then, open the Simulink Coder app.

  2. Select model configuration parameter Generate code only.

  3. Select model configuration parameter Create code generation report and click Apply.

  4. Generate code.

    Messages appear in the Diagnostics Viewer. The code generator produces standard C and header files, and an HTML code generation report. The code generator places the files in a build folder, a subfolder named throttlecntrl_grt_rtw under your current working folder.

Review the Generated Code

  1. In the code generation report, click the link for the generated C model file and review the generated code. Look for these items in the report:

    • Identification, version, timestamp, and configuration comments.

    • Links to help you navigate within and between files

    • Data definitions

    • Scheduler code

    • Controller code

    • Model initialization and termination functions

    • Call interface for the GRT system target file — output, update, initialization, start, and terminate

  2. Save and close model throttlecntrl.

Consider examining the following files. In the code generation report Contents pane, click the links.

throttlecntrl.cC file that contains the scheduler, controller, initialization, and interface code
throttlecntrl_data.cC file that assigns values to generated data structures
throttlecntrl.hHeader file that defines data structures
throttlecntrl_private.hHeader file that defines data used only by the generated code
throttlecntrl_types.hHeader file that defines the model data structure

For more information, see Manage Build Process File Dependencies.

At this point, consider logging data to a MAT-file. For an example, see Log Data for Analysis.

Generate an Executable

  1. Open throttlecntrl. Then, open the Simulink Coder app.

  2. Clear model configuration parameter Generate code only and click Apply.

  3. Build the model. Watch the messages in the Diagnostics Viewer. The code generator uses a template make file associated with your system target file selection to create an executable file. You can run this program on your workstation, independent of external timing and events.

  4. Check your working folder for the file throttlecntrl.exe.

  5. Run the executable. In the Command Window, enter !throttlecntrl. The ! character passes the command that follows it to the operating system, which runs the standalone program.

    The program produces one line of output in the Command Window:

    ** starting the model **

    At this point, consider logging data to a MAT-file. For an example, see Log Data for Analysis.


For UNIX® platforms, run the executable program in the Command Window with the syntax !./executable_name. If preferred, run the executable program from an OS shell with the syntax ./executable_name. For more information, see Run External Commands, Scripts, and Programs.

Key Points

  • To generate code, change the model configuration to specify a fixed-step solver then select a system target file. Using the grt.tlc file requires a fixed-step solver. If the model contains continuous time blocks, you can use a variable-step solver with the rsim.tlc system target file.

  • After debugging a model, consider configuring a model with parameter inlining enabled.

  • Use the getActiveConfigSet function to save a model configuration for future use or to apply it to another model.

  • Before generating code, consider checking a model with the Model Advisor.

  • The code generator places generated files in a subfolder (model_grt_rtw) of your working folder.

Related Topics