Skip to Main Content Skip to Search
Product Documentation

Creating Flow Graphs with the Pattern Wizard

Why Use the Pattern Wizard?

The Pattern Wizard is a utility that generates common flow graph patterns for use in graphical functions and charts. Although you can also create flow graphs by hand, the Pattern Wizard offers several advantages:

How to Create Reusable Flow Graphs

When you create flow graphs with the Pattern Wizard, you can save them to a central location where you can retrieve them for reuse. To create reusable flow graphs that comply with MAAB guidelines:

  1. Open a chart.

     How do I create and open a new Stateflow chart?

  2. Select a flow graph pattern:

    To Create:Select:Reference
    if decision patternsPatterns > Add Decision

    Decision Logic Patterns in Flow Graphs

    for, while, and do while loop patternsPatterns > Add Loop

    Iterative Loop Patterns in Flow Graphs

    switch patternsPatterns > Add Switch

    Switch Patterns in Flow Graphs

    The Stateflow Patterns dialog box appears.

  3. Enter a description of your pattern (optional).

    If you leave this field blank, the Pattern Wizard adds a default description to your chart.

  4. Specify conditions and actions (optional).

    You can also add or change conditions and actions directly in the chart.

  5. Click OK.

    The pattern appears in your chart. The geometry and layout comply with MAAB guidelines.

  6. Customize the pattern as desired.

    For example, you may want to add or change flow graphs, conditions, or actions. See Try It: Creating and Reusing a Custom Pattern with the Pattern Wizard.

  7. Save the pattern to a central location as described in Saving and Reusing Flow Graph Patterns.

You can now retrieve your pattern directly from the editor to reuse in graphical functions and charts. See How to Add Flow Graph Patterns in Graphical Functions and How to Add Flow Graph Patterns in Charts.

Saving and Reusing Flow Graph Patterns

Using the Pattern Wizard, you can save flow graph patterns in a central location, then easily retrieve and reuse them in Stateflow graphical functions and charts. The Pattern Wizard lets you access all saved patterns from the editor.

Guidelines for Creating a Pattern Folder

The Pattern Wizard uses a single, flat folder for saving and retrieving flow graph patterns. Follow these guidelines when creating your pattern folder:

How to Save Flow Graph Patterns for Easy Retrieval

  1. Create a folder for storing your patterns according to Guidelines for Creating a Pattern Folder.

  2. In your chart, select flow graphs with the patterns you want to save.

  3. Select Patterns > Save Pattern.

    The Pattern Wizard displays a message that prompts you to choose a folder for storing custom patterns.

    The Pattern Wizard stores your flow graphs in the pattern folder as an .mdl file. The patterns that you save in this folder appear in a drop-down list when you select Patterns > Add Custom, as described in How to Add Flow Graph Patterns in Graphical Functions and How to Add Flow Graph Patterns in Charts.

  4. Click OK to dismiss the message.

    The Browse For Folder dialog box appears.

  5. Select the designated folder (or create a new folder) and click OK.

    The Save Pattern As dialog box appears.

  6. Enter a name for your pattern and click Save.

    The Pattern Wizard saves your pattern as an .mdl file in the designated folder.

How to Change Your Pattern Folder

  1. Rename your existing pattern folder.

  2. Add a pattern as described in How to Add Flow Graph Patterns in Graphical Functions or How to Add Flow Graph Patterns in Charts.

    The Pattern Wizard prompts you to choose a folder.

  3. Follow the instructions in How to Save Flow Graph Patterns for Easy Retrieval.

How to Add Flow Graph Patterns in Graphical Functions

  1. Add a graphical function to your chart.

    See Creating a Graphical Function.

  2. Make the graphical function into a subchart by right-clicking in the function box and selecting Make Contents > Subcharted.

    The function box turns gray.

  3. Double-click the subcharted graphical function to open it.

  4. In the menu bar, select Patterns > Add Custom.

    The Select a Custom Pattern dialog box appears, displaying all of your saved patterns.

     Why does my dialog box not display any patterns?

  5. Select a pattern from the list in the dialog box and click OK.

    The pattern appears in the graphical function, which expands to fit the flow graph.

  6. Define all necessary inputs, outputs, and local data in the graphical function and the chart that calls it.

How to Add Flow Graph Patterns in Charts

  1. In the menu bar, select Patterns > Add Custom.

    The Select a Custom Pattern dialog box appears, displaying all of your saved patterns.

  2. Select a pattern from the list in the dialog box and click OK.

    The pattern appears in the chart.

  3. Adjust the chart by hand to:

    • Connect the flow graphs to the appropriate transitions.

    • Ensure that there is only one default transition for exclusive (OR) states at each level of hierarchy.

    • Define all necessary inputs, outputs, and local data.

MAAB-Compliant Patterns from the Pattern Wizard

The Pattern Wizard generates MAAB-compliant flow graphs.

Decision Logic Patterns in Flow Graphs

The Pattern Wizard generates the following MAAB-compliant decision logic patterns:

if

if-else

if-elseif

if-elseif-else

if-elseif-elseif-else

Nested if

Iterative Loop Patterns in Flow Graphs

The Pattern Wizard generates the following MAAB-compliant iterative loop patterns:

for

while

do-while

Switch Patterns in Flow Graphs

The Pattern Wizard generates the following MAAB-compliant switch patterns:

switch with two cases and default

switch with three cases and default

switch with four cases and default

Try It: Creating and Reusing a Custom Pattern with the Pattern Wizard

This exercise shows how to create, modify, and save a custom flow graph pattern for iterating over the upper triangle of a two-dimensional matrix. In the upper triangle, the row index i is always less than or equal to column index j. This flow graph pattern uses nested for-loops to ensure that i never exceeds j.

Creating the Upper Triangle Iterator Pattern

  1. Open a new (empty) chart.

  2. Select Patterns > Add Loop > For.

    The Stateflow Patterns dialog box appears.

  3. Enter the initializer, loop test, and counting expressions for iterating through the first dimension of the matrix, as follows:

    Do not specify an action yet. You will add another loop for iterating the second dimension of the matrix.

  4. Click OK.

    The Pattern Wizard generates the first iterative loop in your chart:

    This pattern from the Pattern Wizard:

  5. Add the second loop by following these steps:

    1. Expand the editor window so the chart can accommodate a second pattern.

    2. Deselect all objects in the chart.

    3. Repeat steps 2, 3, and 4, this time specifying parameters for the second iterator j, and a placeholder for an action to retrieve each element in the upper triangle.

    The Pattern Wizard generates the second loop pattern and leaves it selected so you can reposition it.

  6. Nest the loop patterns as follows:

     How can I nest the loop patterns?

  7. Inspect the flow graph to ensure that:

    • There is only one default transition, attached to the first for-loop.

    • You order the transitions as shown above.

       What if the ordering is not correct?

  8. Save your chart.

Now you are ready to save your pattern to a central location for reuse (see Saving the Upper Triangle Iterator Pattern for Reuse).

Saving the Upper Triangle Iterator Pattern for Reuse

  1. Create a folder for storing flow graph patterns, as described in Guidelines for Creating a Pattern Folder.

  2. Open the chart that contains the custom pattern.

  3. In the chart, select the flow graph with the pattern that you want to save.

  4. In the editor, select Patterns > Save Pattern and take one of these actions:

    If you have...Then Pattern Wizard...Action
    Not yet designated the pattern folderPrompts you to create or select a pattern folderSelect the folder you just created. See How to Save Flow Graph Patterns for Easy Retrieval.
    Already designated the pattern folderPrompts you to save your pattern to the designated folderName your pattern and click Save.

    The Pattern Wizard automatically saves your pattern as an .mdl file under the name you specify.

Adding the Upper Triangle Iterator Pattern to a Graphical Function

  1. Open a new chart.

  2. Drag a graphical function into the chart from the object palette and enter the following function signature:

    function y = ut_iterator(u, numrow, numcol)

    The function takes three inputs:

    InputDescription
    u2-D matrix
    numrowNumber of rows in the matrix
    numcolNumber of columns in the matrix

  3. Right-click inside the function and select Make Contents > Subcharted.

    The function should look like this:

  4. Double-click to open the subcharted function and select Patterns > Add Custom.

    The Select a Custom Pattern dialog box appears, listing all the patterns you saved in your pattern folder.

  5. Select your upper triangle iterator pattern and click OK.

    The Pattern Wizard adds your custom pattern to the graphical function.

Before calling this function from a chart, be sure to modify data names, types, and sizes as necessary and substitute an appropriate action.

  


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