メインコンテンツ

Pattern Wizard

Create decision, loop, and switch patterns in Stateflow

Description

Use the Pattern Wizard to create and manage flow chart patterns in Stateflow® charts and graphical functions. You can use the built-in decision, loop, and switch patterns to create structures that comply with MAB guidelines. You can save custom patterns for reuse, extend existing flow charts with new logic patterns, and convert MATLAB® code into graphical representations. Pattern options include:

  • If, If-Else, If-Elseif, and other nested decision patterns

  • For-, While-, and DoWhile- loop patterns

  • Switch patterns with up to four cases

  • Custom patterns

  • Patterns that you define in a MATLAB program file

The Stateflow Pattern Wizard.

Open the Pattern Wizard

  • To open the Pattern Wizard, open a Stateflow chart. In the Modeling tab, select Pattern.

    Pattern options for the Pattern Wizard.

Examples

expand all

To create an If-Else flow chart:

  1. On the Modeling tab, select Pattern > If-Else.

  2. In the Stateflow Pattern dialog box, enter x > 0 in the If condition field.

  3. Enter y = 1; in the If action field and y = 0; in the Else action field.

  4. Click OK. The Pattern Wizard generates an If-Else flow chart with the specified condition and actions.

    If-else pattern created with the Pattern Wizard.

    After you create a pattern, you can customize it by modifying the conditions and actions or by inserting additional logic patterns.

  1. In the Stateflow Editor, select a transition in your chart where you want to insert the loop.

  2. On the Modeling tab, select Pattern > For Loop.

  3. In the Stateflow Pattern dialog box, set Initializer expression to i = 1, Loop test expression to i <= 5, and Counting expression to i = i + 1.

  4. To execute the loop in each iteration, set Foor loop body to sum = sum + i;

  5. Click OK. The Pattern Wizard inserts the For- loop at the selected transition.

    For-loop pattern created with the Pattern Wizard.

To save a custom pattern that iterates through the upper triangle of a matrix:

  1. Create a flow chart with nested For- loops where the inner loop starts at the value of the outer loop index.

    The upper triangle iterator.

  2. In the Stateflow Editor, select the entire flow chart pattern.

  3. On the Modeling tab, select Pattern > Save As Pattern.

  4. Name your pattern UpperTriangleIterator and click Save.

To use a custom pattern:

  1. On the Modeling tab, select Pattern > Select pattern.

  2. Select UpperTriangleIterator from the list and click OK.

  3. The Pattern Wizard adds the upper triangle iterator pattern to the chart.

In standalone Stateflow charts, you can convert:

  • If, If-Else, and other nested decision statements

  • For- and While- loops

  • Switch statements

To convert MATLAB code into Stateflow flow charts:

First, create the Collatz conjuncture function from Convert MATLAB Code into Stateflow Flow Charts. Save the function file collatz.m. The Collatz conjeecture function computes the collatz sequence for a given number.

  1. On the Modeling tab, select Pattern > Select File.

  2. Choose a MATLAB file containing the Collatz conjecture function that computes the sequence for a given number. For more information about the Collatz conjecture, see Convert MATLAB Code into Stateflow Flow Charts.

  3. Click Open. The Pattern Wizard creates a flow chart by using the while- loop and If-Else structure to implement the algorithm.

Limitations

  • You can only convert MATLAB code to flow charts in standalone Stateflow charts.

Version History

Introduced in R2008b