| Contents | Index |
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:
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
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:
Open a chart.
Select a flow graph pattern:
| To Create: | Select: | Reference |
|---|---|---|
| if decision patterns | Patterns > Add Decision | |
| for, while, and do while loop patterns | Patterns > Add Loop | |
| switch patterns | Patterns > Add Switch |
The Stateflow Patterns dialog box appears.
Enter a description of your pattern (optional).
If you leave this field blank, the Pattern Wizard adds a default description to your chart.
Specify conditions and actions (optional).
You can also add or change conditions and actions directly in the chart.
Click OK.
The pattern appears in your chart. The geometry and layout comply with MAAB guidelines.
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.
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.
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.
The Pattern Wizard uses a single, flat folder for saving and retrieving flow graph patterns. Follow these guidelines when creating your pattern folder:
Store all flow graphs at the top level of the pattern folder; do not create subfolders.
Make sure all flow graph files have a .mdl extension.
Create a folder for storing your patterns according to Guidelines for Creating a Pattern Folder.
In your chart, select flow graphs with the patterns you want to save.
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.
Click OK to dismiss the message.
The Browse For Folder dialog box appears.
Select the designated folder (or create a new folder) and click OK.
The Save Pattern As dialog box appears.
Enter a name for your pattern and click Save.
The Pattern Wizard saves your pattern as an .mdl file in the designated folder.
Rename your existing pattern folder.
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.
Follow the instructions in How to Save Flow Graph Patterns for Easy Retrieval.
Add a graphical function to your chart.
Make the graphical function into a subchart by right-clicking in the function box and selecting Make Contents > Subcharted.
The function box turns gray.
Double-click the subcharted graphical function to open it.
In the menu bar, select Patterns > Add Custom.
The Select a Custom Pattern dialog box appears, displaying all of your saved patterns.
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.
Define all necessary inputs, outputs, and local data in the graphical function and the chart that calls it.
In the menu bar, select Patterns > Add Custom.
The Select a Custom Pattern dialog box appears, displaying all of your saved patterns.
Select a pattern from the list in the dialog box and click OK.
The pattern appears in the chart.
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.
The Pattern Wizard generates MAAB-compliant 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

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

while

do-while

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

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.
Open a new (empty) chart.
Select Patterns > Add Loop > For.
The Stateflow Patterns dialog box appears.
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.
Click OK.
The Pattern Wizard generates the first iterative loop in your chart:

This pattern from the Pattern Wizard:
Conforms to all best practices for creating flow graphs, as described in Best Practices for Creating Flow Graphs.
Provides the correct syntax for conditions and condition actions.
Add the second loop by following these steps:
Expand the editor window so the chart can accommodate a second pattern.
Deselect all objects in the chart.
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.
Nest the loop patterns as follows:

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.
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).
Create a folder for storing flow graph patterns, as described in Guidelines for Creating a Pattern Folder.
Open the chart that contains the custom pattern.
In the chart, select the flow graph with the pattern that you want to save.
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 folder | Prompts you to create or select a pattern folder | Select the folder you just created. See How to Save Flow Graph Patterns for Easy Retrieval. |
| Already designated the pattern folder | Prompts you to save your pattern to the designated folder | Name your pattern and click Save. |
The Pattern Wizard automatically saves your pattern as an .mdl file under the name you specify.
Open a new chart.
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:
| Input | Description |
|---|---|
| u | 2-D matrix |
| numrow | Number of rows in the matrix |
| numcol | Number of columns in the matrix |
Right-click inside the function and select Make Contents > Subcharted.
The function should look like this:

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.

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.
![]() | When to Use Flow Graphs | Drawing and Customizing Flow Graphs By Hand | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |