Main Content

Prepare a Model for Code Generation

Several standard methods are available for setting up a model to generate specific C constructs in your code. For preparing your model for code generation, some of these methods include: configuring signals and ports, initializing states, and setting up configuration parameters for code generation. Depending on the components of your model, some of these methods are optional. Methods for configuring a model to generate specific C constructs are organized by category, for example, the Control Flow category includes constructs if-else, switch, for, and while. Refer to the name of a construct to see how you should configure blocks and parameters in your model. Different modeling methodologies are available, such as Simulink® blocks, Stateflow® charts, and MATLAB Function blocks, to implement a C construct.

Model examples in Modeling Patterns for C Code have the following naming conventions:

Model ComponentsNaming Convention
Inputsu1, u2, u3, and so on
Outputsy1, y2, y3, and so on
Parametersp1, p2, p3, and so on
Statesx1, x2, x3, and so on

Input ports are named to reflect the signal names that they propagate.

Configure Input Ports, Output Ports, and Arbitrary Signals

  1. Create a model in Simulink. For more information, see Interactive Model Editing.

  2. On the Modeling tab, click Model Data Editor. You can also use the keyboard shortcut Ctrl + Shift + E.

  3. Set the Change view drop-down list to Design.

  4. Use the Storage Class column to apply a storage class to the signal.

    For example, apply the storage class ExportedGlobal, which makes the signal appear in the generated code as a separate global variable. The variable has the same name as the signal in the model.

Initialize and Configure States

  1. In the Model Data Editor, on the States tab, use the Initial Value column to specify initial values for a block state (such as the state of a Unit Delay block).

  2. Set the Change view drop-down list to Code.

  3. Use the Name column to give the state a name.

  4. Use the Storage Class column to apply a storage class to the state.

Set Up Configuration Parameters for Code Generation

  1. On the C Code tab, click Settings to open the Configuration Parameters dialog box. You can also use the keyboard shortcut Ctrl + E.

    Open the Configuration Parameter dialog box by selecting Simulation > Model Configuration parameters.

  2. Open the Solver pane and select

    • Solver type: Fixed-Step

    • Solver: discrete (no continuous states)

  3. Open the Optimization pane, and set Default parameter behavior to Inlined.

  4. Open the Code Generation pane, and specify ert.tlc as the System Target File.

  5. Clear Generate makefile.

  6. Select Generate code only.

  7. Enable the HTML report generation by opening the Code Generation > Report pane and selecting Create code generation report and Open report automatically. Click the horizontal ellipsis and, under Advanced parameters, select Code-to-model. Enabling the HTML report generation is optional.

  8. Click Apply and then OK to exit.

Set Up an Example Model With a Stateflow Chart

Follow this general procedure to create a simple model containing a Stateflow chart.

  1. From the Stateflow > Chart library, add a Stateflow chart to your model .

  2. Add Inport blocks and Outport blocks according to the example model.

  3. Open the Stateflow Editor by performing one of the following:

    • Double-click the Stateflow chart.

    • Press Ctrl+R.

  4. Add inputs to you chart as described in Add Stateflow Data (Stateflow).

  5. Specify the Name (u1, u2, ...) and the Type (Inherit: Same as Simulink) for each input, unless specified differently in the example.

  6. Add outputs to you chart as described in Add Stateflow Data (Stateflow).

  7. Specify the Name (y1, y2, ...) and Type (Inherit: Same as Simulink) for each output, unless specified differently in the example.

  8. In the Stateflow Editor, create the Stateflow diagram specific to the example.

  9. The inputs and outputs appear on the chart in your model.

  10. Connect the Inport and Outport blocks to the Stateflow Chart.

  11. Configure the input and output signals; see Configure Input Ports, Output Ports, and Arbitrary Signals.

Set Up an Example Model With a MATLAB Function Block

  1. Add the number of Inport and Outport blocks according to a C construct example included in this chapter.

  2. From the Simulink User-defined Functions library drag a MATLAB Function block into the model.

  3. Double-click the block. The MATLAB Function Block Editor opens. Edit the function to implement your application.

  4. On the Editor tab, click Save and close the MATLAB Function Block Editor.

  5. Connect the Inport and Outport blocks to the MATLAB Function block. See Configure Input Ports, Output Ports, and Arbitrary Signals.

  6. Save your model.