This example shows how to use atomic subcharts to generate code for individual parts of a Stateflow® chart. When you generate code for your chart, a separate file stores the code for the atomic subchart. Atomic subcharts are supported only in Stateflow charts in Simulink® models. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.
This model contains two Sine Wave blocks: one with a frequency of 1 radian per second, and the other with a frequency of 2 radians per second.
In the chart, each state uses saturator logic to convert the input sine wave to an output square wave of the same frequency.
Because this example does not use atomic subcharts, generated code for the entire model is stored in one file. To find code for a specific part of the chart, you have to look through the entire file.
In contrast, you can convert state
A to an atomic subchart and specify that the code for the subchart appears in a separate file. This method of code generation enables unit testing for a specific part of a chart. You avoid searching through unrelated code and focus only on the code that interests you.
Step 1: Convert a State to an Atomic Subchart
A and select Group & Subchart > Atomic Subchart. State
A changes to an atomic subchart and displays the label Atomic in the upper-left corner.
Step 2: Set Up a Standalone C File for the Atomic Subchart
Open the properties dialog box for subchart
A by right-clicking the subchart and selecting Properties.
Set the Code generation function packaging property to
Set the Code generation file name options property to
In the Code generation file name box, enter
saturator as the name of the file.
Step 3: Set Up the Code Generation Report
In the Modeling tab, click Chart Properties > Model Settings to open the Configuration parameters dialog box.
In the Code Generation tab, set the System target file parameter to
Under Code Generation > Report, select Create code generation report.
Under Advanced parameters, select Model-to-code.
Step 4: Customize the Generated Function Names
In the Configuration Parameters dialog box, in the Code Generation > Identifiers tab, set the Subsystem methods parameter to the format scheme
$R is the root model name.
$N is the block name.
$M is the mangle token.
$F is the type of interface function for the atomic subchart.
To generate code for your model by using Embedded Coder®, press Ctrl+B.
The code generation report contains links to the code generated from the chart (Model files) and the atomic subchart (Subsystem files). To inspect the code for the subchart, click the
Line 31 shows that the
during function generated for the atomic subchart has the name
ex_reuse_states_A_during. This name follows the format scheme
$R$N$M$F specified for Subsystem methods:
The root model name is
The block name is
The mangle token is empty.
The type of interface function for the atomic subchart is
Note: The line numbers that appear in your code generation report can differ from the numbers shown.