This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Reuse a State Multiple Times in a Chart

An atomic subchart is a graphical object that helps you to create standalone subcomponents in a Stateflow® chart. Atomic subcharts are supported only in Stateflow charts in Simulink® models. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Goal of the Tutorial

Consider this 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.

Edit a Model to Use Atomic Subcharts

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.

StepTaskReference
1Change one of the states into an atomic subchart.Convert a State to an Atomic Subchart
2Create a library that contains this atomic subchart.Create a Library for the Atomic Subchart
3Replace the states in your chart with linked atomic subcharts.Replace States with Linked Atomic Subcharts
4Edit the mapping of input and output variables where necessary.Edit the Mapping of Input and Output Variables

Tip

You can skip the conversion steps and access the new model directly.

Convert a State to an Atomic Subchart

To convert state A to an atomic subchart, right-click the state and select Group & Subchart > Atomic Subchart. State A changes to an atomic subchart:

Create a Library for the Atomic Subchart

To enable reuse of the atomic subchart you created in Convert a State to an Atomic Subchart, store the atomic subchart in a library:

  1. Create a new library model.

  2. 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.

  3. Save your library model.

Replace States with Linked Atomic Subcharts

To replace the states in your chart with linked atomic subcharts:

  1. Delete both states from the chart.

  2. Copy the atomic subchart in your library and paste in your chart twice.

  3. Rename the second instance as B.

    Each linked atomic subchart appears opaque and contains the label Link in the upper-left corner.

Edit the Mapping of Input and Output Variables

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

You also see warnings about unused data. 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:

  1. Open the properties dialog box for B.

  2. Click the Mappings tab.

  3. 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.

  4. 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.

  5. Click OK.

Run the New Model

When you simulate the new model, the results match those of the original design.

Propagate a Change in the Library Chart

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.