| Contents | Index |
| On this page… |
|---|
This tutorial is available as a video and a hands-on tutorial. Select one of the following options:
Try it yourself
Watch the video in your default Web browser
Assume that you have the following model:

The top Sine Wave block uses a frequency of 1 radian per second, and the bottom Sine Wave block uses a frequency of 2 radians per second. The blocks use the same amplitude (1) and phase shift (0).
In the chart, each state uses saturator logic to convert the input sine wave to an output square wave of the same frequency. The states perform the same actions and differ only in the names of input and output data:

When you run the model, you get the following results:

Suppose that you want to reuse the contents of state A in the chart. You can convert that state to an atomic subchart and then use multiple linked instances of that subchart in your chart.
The sections that follow describe how to replace states in your chart with atomic subcharts. This procedure enables reuse of the same object in your model while retaining the same simulation results.
| Step | Task | Reference |
|---|---|---|
| 1 | Change one of the states into an atomic subchart. | Converting a State to an Atomic Subchart |
| 2 | Create a library that contains this atomic subchart. | Creating a Library for the Atomic Subchart |
| 3 | Replace the states in your chart with linked atomic subcharts. | Replacing States with Linked Atomic Subcharts |
| 4 | Edit the mapping of input and output variables where necessary. | Editing the Mapping of Input and Output Variables |
Tip You can skip the conversion steps and access the new model directly. |
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:

To enable reuse of the atomic subchart you created in Converting a State to an Atomic Subchart, store the atomic subchart in a library:
Create a new library model.
Copy the atomic subchart and paste in your library.
The atomic subchart appears as a standalone chart with an input and an output. This standalone property enables you to reuse the contents of the atomic subchart.

Save your library model.
To replace the states in your chart with linked atomic subcharts:
Delete both states from the chart.
Copy the atomic subchart in your library and paste in your chart twice.
Rename the second instance as B.
Each linked atomic subchart appears opaque and contains the label (Link) in the upper-left corner.

If you simulate the model now, the output for y2 is zero:

You also see warnings about unused data in the Simulation Diagnostics Viewer. These warnings appear because atomic subchart B uses u1 and y1 instead of u2 and y2. To fix these warnings, you must edit the mapping of input and output variables:
Open the properties dialog box for B.
Click the Mappings tab.

Under Input Mapping, select u2 from the drop-down list.
The input variable in your atomic subchart now maps to the correct input variable in the main chart.
Under Output Mapping, select y2 from the drop-down list.
The output variable in your atomic subchart now maps to the correct output variable in the main chart.
Click OK.
When you simulate the new model, the results match those of the original design.

Suppose that you edit the transition from Pos to Neg in the library chart:

This change propagates to all linked atomic subcharts in your main chart. You do not have to update each state individually.
![]() | Rules for Using Atomic Subcharts in Stateflow Charts | Tutorial: Reducing the Compilation Time of a Chart | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |