Skip to Main Content Skip to Search
Product Documentation

Tutorial: Generating Reusable Code for Unit Testing

Goal of the Tutorial

Assume that you have the following model, and the chart has two states:

Suppose that you want to generate reusable code so that you can perform unit testing on state A. You can convert that part of the chart to an atomic subchart and then specify a separate file to store the Simulink Coder generated code.

Converting a State to an Atomic Subchart

To convert state A to an atomic subchart, right-click the state and select Make Contents > Atomic Subcharted. State A changes to an atomic subchart:

Specifying Code Generation Parameters

Setting Up a Standalone C File for the Atomic Subchart

  1. Open the properties dialog box for A.

  2. Set Code generation function packaging to Reusable function.

  3. Set Code generation file name options to User specified.

  4. For Code generation file name, enter saturator as the name of the file.

  5. Click OK.

Setting Up the Code Generation Report

  1. Open the Configuration Parameters dialog box.

  2. In the Code Generation pane, set System target file to ert.tlc.

  3. In the Code Generation > Report pane, select Create code generation report.

    This step automatically selects Open report automatically and Code-to-model.

  4. Select Model-to-code.

  5. Click Apply.

Customizing the Generated Function Names

  1. In the Configuration Parameters dialog box, go to the Code Generation > Symbols pane.

  2. Set Subsystem methods to the format string $R$N$M$F, where:

    • $R is the root model name.

    • $N is the block name.

    • $M is the mangle string.

    • $F is the type of interface function for the atomic subchart.

    For more information, see Subsystem methods in the Simulink Coder documentation.

  3. Click Apply.

Generating Code for Only the Atomic Subchart

To generate code for your model, click Build on the Code Generation pane of the Configuration Parameters dialog box. In the code generation report that appears, you see a separate file that contains the generated code for the atomic subchart.

To inspect the code for saturator.c, click the hyperlink in the report to see the following code:

Line 28 shows that the during function generated for the atomic subchart has the name ex_reuse_states_A_during. This name follows the format string $R$N$M$F specified for Subsystem methods:

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS