Skip to Main Content Skip to Search
Product Documentation

Tutorial: Reusing a State Multiple Times in a Chart

Ways to Use This Tutorial

This tutorial is available as a video and a hands-on tutorial. Select one of the following options:

Goal of the Tutorial

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.

Editing 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.Converting a State to an Atomic Subchart
2Create a library that contains this atomic subchart.Creating a Library for the Atomic Subchart
3Replace the states in your chart with linked atomic subcharts.Replacing States with Linked Atomic Subcharts
4Edit the mapping of input and output variables where necessary.Editing the Mapping of Input and Output Variables

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:

Creating a Library for the 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:

  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.

      Note   You cannot drag and drop the atomic subchart into your library model. Only a copy-and-paste operation works.

  3. Save your library model.

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

Editing 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 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:

  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.

Running the New Model

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

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

  


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