Create Flow Charts with the Pattern Wizard

Why Use the Pattern Wizard?

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

  • Generates common logic and iterative loop patterns automatically

  • Generates patterns that comply with guidelines from the MathWorks Automotive Advisory Board (MAAB)

  • Promotes consistency in geometry and layout across patterns

  • Facilitates storing and reusing patterns from a central location

  • Provides ability to insert patterns in existing flow graph

    Note:   The Pattern Wizard is only used for flow charts, and cannot be used to save states and subcharts. Atomic subcharts can be used to reuse states and subcharts.

How to Create Reusable Flow Charts

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

  1. Open a chart.

     How do I create and open a new Stateflow chart?

  2. Select a flow chart pattern:

    To Create:Select:Reference
    if decision patternsChart > Add Pattern in Chart > Decision

    Decision Logic Patterns in Flow Charts

    for-, while-, and do-while-loop patternsChart > Add Pattern in Chart > Loop

    Iterative Loop Patterns in Flow Charts

    switch patternsChart > Add Pattern in Chart > Switch

    Switch Patterns in Flow Charts

    The Stateflow® Patterns dialog box appears.

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

  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 charts, conditions, or actions. See Create and Reuse a Custom Pattern with the Pattern Wizard.

  7. Save the pattern to a central location as described in Save and Reuse Flow Chart Patterns.

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

Insert a Logic Pattern Using the Pattern Wizard

Using the Pattern Wizard, you can add loop or decision logic extensions to a previously created pattern in a flow chart. Select an eligible vertical transition, and then select Chart > Insert Pattern on Selection. After you select one of the decision or loop patterns, the Pattern Wizard places the new pattern below the action along the transition path.

When you create logic extensions, the following rules apply:

  • Select only one exactly vertical transition to extend at a time.

  • Select a vertical transition that has a destination junction.

  • Extend only a flow chart that was created by the Pattern Wizard.

  • Extend only a flow chart that has junctions and transitions in the chart, not other objects.

  • Do not extend a pattern that has been custom-created or modified.

  • You cannot choose a custom pattern as the extension.

If your selection is not eligible, when you select Chart > Insert Pattern on Selection, you see a message instead of pattern options.

MessageIssue
Select a vertical transitionYou have not selected a vertical transition.
Selected transition must be exactly verticalYou selected a transition, but it is not vertical.
Select only one vertical transitionYou have selected more than one transition.
Editor must contain only transitions and junctionsThere are other objects, such as states, functions or truth tables in the editor.

Insert a Pattern

In this example, you add an if-else pattern into a while-loop body.

  1. Open a chart.

  2. Select Chart > Add Pattern in Chart > Loop > While.

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

  4. Specify conditions and actions (optional).

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

  5. Click OK.

    The while pattern appears in your chart.

  6. Select the vertical transition labeled {action1}.

  7. Select Chart > Insert Pattern on Selection > Decision > If-Else.

  8. Click OK.

    The if-else pattern is added to the while-loop below {action1}.

Save and Reuse Flow Chart Patterns

Using the Pattern Wizard, you can save flow chart 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 chart patterns. Follow these guidelines when creating your pattern folder:

  • Store all flow charts at the top level of the pattern folder; do not create subfolders.

  • Make sure all flow chart files have a .mdl or .slx extension.

How to Save Flow Chart 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 charts with the patterns you want to save.

  3. Select Chart > 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 charts in the pattern folder as a model file. The patterns that you save in this folder appear in a drop-down list when you select Chart > Add Pattern in Chart > Custom, as described in How to Add Flow Chart Patterns in Graphical Functions and How to Add Flow Chart 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 a model 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 Chart Patterns in Graphical Functions or How to Add Flow Chart Patterns in Charts.

    The Pattern Wizard prompts you to choose a folder.

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

How to Add Flow Chart Patterns in Graphical Functions

  1. Add a graphical function to your chart.

    See Create a Graphical Function.

  2. Make the graphical function into a subchart by right-clicking in the function box and selecting Group & Subchart > Subchart.

    The function box turns gray.

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

  4. In the menu bar, select Chart > Add Pattern in Function > 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 chart.

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

How to Add Flow Chart Patterns in Charts

  1. In the menu bar, select Chart > Add Pattern in Chart > 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 charts 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 charts.

Decision Logic Patterns in Flow Charts

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 Charts

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

for

while

do-while

Switch Patterns in Flow Charts

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

Create and Reuse a Custom Pattern with the Pattern Wizard

This example shows how to create, modify, and save a custom flow chart 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 chart pattern uses nested for-loops to ensure that i never exceeds j.

Create the Upper Triangle Iterator Pattern

  1. Open a new (empty) chart.

  2. Select Chart > Add Pattern in Chart > Loop > For.

  3. In the Stateflow Patterns dialog box, 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:

  5. Add the second loop:

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

    2. Select the vertical transition labelled {action1}.

    3. Select Chart > Insert Pattern on Selection > Loop > For.

    4. Enter the initializer, loop test, and counting expressions for the second iterator j, and a placeholder for an action to retrieve each element in the upper triangle as follows:

    5. Click OK. The Pattern Wizard adds the second loop to the first loop.

  6. Save your chart.

Save your pattern to a central location for reuse (see Save the Upper Triangle Iterator Pattern for Reuse).

Save the Upper Triangle Iterator Pattern for Reuse

  1. Create a folder for storing flow chart 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 chart with the pattern that you want to save.

  4. In the editor, select Chart > 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 Chart 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 a model file under the name that you specify.

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

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

  4. Right-click inside the function and select Group & Subchart > Subchart.

    The function looks like this graphic.

  5. Double-click to open the subcharted function and select Chart > Add Pattern in Function > Custom.

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

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

Was this topic helpful?