Create Flow Charts by Using Pattern Wizard

The Pattern Wizard is a utility that generates common flow chart patterns for use in graphical functions and charts. The Pattern Wizard offers several advantages over manually creating flow charts. The Pattern Wizard:

  • Generates common logic and iterative loop patterns.

  • Promotes consistency in geometry and layout across patterns.

  • Facilitates storing and reusing patterns from a central location.

  • Allows inserting patterns in an existing flow chart.

The Pattern Wizard generates flow charts whose geometry and layout comply with the guidelines from the MathWorks Automotive Advisory Board (MAAB). You can customize your flow chart by modifying the conditions and actions or by inserting additional logic patterns. You can also save your flow chart as a custom pattern in the Pattern Wizard for later reuse.

For example, suppose that you want to use the Pattern Wizard to create a graphical function for iterating over the upper triangle of a two-dimensional matrix. The function consists of two nested for loops in which the row index i is always less than or equal to the column index j. By using the Pattern Wizard, you can:

  1. Create a flow chart for the outer loop that iterates over the row index i.

  2. Extend the flow chart by inserting an inner loop that iterates over the column index j.

  3. Save the flow chart as a custom pattern in the Pattern Wizard.

  4. Reuse the custom pattern in a graphical function.

Create Reusable Flow Charts

To create a flow chart, select a pattern from the Chart > Add Pattern in Chart menu. Pattern selections include:

  • Decision: if, if-else, and nested if decision patterns.

  • Loop: for, while, and do-while loop patterns.

  • Switch: switch patterns with up to four cases.

  • Custom: custom patterns that you saved for later reuse.

The Stateflow® Patterns dialog box prompts you for conditions and actions specific to the pattern that you select. For more information on flow chart patterns, see MAAB-Compliant Patterns from the Pattern Wizard.

For example, to create the outer for loop in the upper triangle iterator pattern:

  1. In the Stateflow editor, select Chart > Add Pattern in Chart > Loop > For.

  2. In the Stateflow Patterns dialog box, specify the initializer, loop test, and counting expressions for iterating through the first dimension of the matrix:

  3. Click OK. The Pattern Wizard generates this flow chart.

To complete the upper triangle iterator pattern, insert a second for loop along the vertical transition in this flow chart.

Insert Logic Patterns in Existing Flow Charts

Use the Pattern Wizard to add loop or decision logic extensions to an existing flow chart. Select an eligible vertical transition and choose a pattern from the Chart > Insert Pattern on Selection menu. Available patterns include Decision, Loop, and Switch. The Stateflow Patterns dialog box prompts you for conditions and actions specific to the pattern that you select.

For example, to add the second loop in the upper triangle iterator pattern:

  1. In the Stateflow editor, from the outer for loop pattern, select the vertical transition labeled {action1}.

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

  3. In the Stateflow Patterns dialog box, specify the initializer, loop test, and counting expressions for iterating through the second dimension of the matrix. The initializer expression ensures that i never exceeds j. Also enter an action that retrieves each element in the upper triangle of the matrix.

  4. Click OK. The Pattern Wizard adds the second loop to the flow chart.

  5. Save the model containing the pattern.

Guidelines for Inserting Logic Patterns

When you create logic extensions:

  • You can select only one transition to extend at a time. The selected transition must be exactly vertical and have a destination junction.

  • You can extend only flow charts created by the Pattern Wizard.

  • The Stateflow chart containing the flow chart can contain only junctions and transitions. The chart cannot contain other objects, such as states, functions, or truth tables.

  • You cannot 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 for insertion, 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 chart.

Save Custom Flow Chart Patterns

Use the Pattern Wizard to save flow chart patterns in a central location, and then easily retrieve them for reuse in graphical functions and charts. Select the flow chart with the pattern that you want to save and select Chart > Save Pattern.

For example, suppose that you want to save the upper triangle iterator pattern for later reuse:

  1. Create a folder for storing your custom patterns. See Guidelines for Creating a Custom Pattern Folder.

  2. In the Stateflow editor, select the upper triangle iterator flow chart.

  3. Select Chart > Save Pattern.

  4. If you have not designated the custom pattern folder, the Pattern Wizard prompts you to select a folder. Choose the folder that you created and click Select Folder.

  5. At the prompt, name your pattern UpperTriangleIterator and click Save. The Pattern Wizard saves your pattern as a model file UpperTriangleIterator.slx in the custom pattern folder.

Note

You can use the Pattern Wizard to reuse only flow charts. To reuse states and subcharts, create an atomic subchart. For more information, see Create Reusable Subcomponents by Using Atomic Subcharts.

Guidelines for Creating a Custom Pattern Folder

The Pattern Wizard uses a single, flat folder for saving and retrieving flow chart patterns.

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

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

Change Your Custom Pattern Folder

The Pattern Wizard remembers your choice of custom pattern folder for future sessions. To choose a different folder, rename your existing custom pattern folder and do one of the following:

  • Save a new custom pattern to the Pattern Wizard.

  • Reuse an existing custom pattern from the Pattern Wizard.

The Pattern Wizard prompts you to choose a new folder.

Reuse Custom Flow Chart Patterns

The Pattern Wizard stores your flow charts as model files in the custom pattern folder. The patterns that you save in this folder appear in a drop-down list when you select Chart > Add Pattern in Chart > Custom. You can add a custom pattern directly to a chart or to a subcharted graphical function in your chart.

For example, to add the upper triangle iterator custom pattern to a graphical function:

  1. From the object palette, add a graphical function to your chart as described in Define a Graphical Function.

  2. Enter this 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 Group & Subchart > Subchart. The function appears as an opaque box.

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

  5. Select Chart > Add Pattern in Function > Custom. A dialog box opens, listing all the patterns that you have saved in your custom pattern folder.

  6. Select the upper triangle iterator pattern and click OK. The Pattern Wizard adds your custom pattern to the graphical function.

  7. In the graphical function, in place of action1, substitute an appropriate action. This action repeats once for every row of the matrix.

MAAB-Compliant Patterns from the Pattern Wizard

The Pattern Wizard generates flow charts whose geometry and layout comply with the guidelines from the MathWorks Automotive Advisory Board (MAAB).

Decision Logic Patterns

 if

 if-else

 if-elseif

 if-elseif-else

 if-elseif-elseif-else

 Nested if

Iterative Loop Patterns

 for

 while

 do-while

Switch Patterns

 switch with Two Cases and Default

 switch with Three Cases and Default

 switch with Four Cases and Default

Related Topics