Create Flow Charts by Using Pattern Wizard - MATLAB & Simulink (original) (raw)

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:

The Pattern Wizard generates flow charts whose geometry and layout comply with the guidelines from the MathWorks Advisory Board (MAB). 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_. See Create Reusable Flow Charts.
  2. Extend the flow chart by inserting an inner loop that iterates over the column index j. See Insert Logic Patterns in Existing Flow Charts.
  3. Save the flow chart as a custom pattern in the Pattern Wizard. See Save Custom Flow Chart Patterns.
  4. Reuse the custom pattern in a graphical function. See Reuse Custom Flow Chart Patterns.

Create Reusable Flow Charts

To create a flow chart, on the Modeling tab, select a pattern from the gallery. Pattern selections include:

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

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

  1. On the Modeling tab, select > .
  2. In the Pattern dialog box, specify the initializer, loop test, and counting expressions for iterating through the first dimension of the matrix:
    Pattern dialog box.
  3. Click OK. The Pattern Wizard generates this flow chart.
    Flow chart that models a for loop.

To complete the upper triangle iterator pattern, insert a secondfor 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 gallery. Options include decision, loop, and switch patterns. The Pattern 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. On the Modeling tab, select > .
  3. In the Pattern 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.
    Pattern dialog box.
  4. Click OK. The Pattern Wizard adds the second loop to the flow chart.
    Flow chart that models two nested for loops.
  5. Save the model containing the pattern.

Guidelines for Inserting Logic Patterns

When you create logic extensions:

If your selection is not eligible for insertion, when you choose a pattern from the gallery, you see a message instead of pattern options.

Message Issue
Select a vertical transition You have not selected a vertical transition.
Selected transition must be exactly vertical You selected a transition, but it is not vertical.
Select only one vertical transition You have selected more than one transition.
Editor must contain only transitions and junctions There 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 Pattern >Save As 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. On the Modeling tab, selectPattern > Save As 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 clickSelect 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.

Guidelines for Creating a Custom Pattern Folder

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

Change Your Custom Pattern Folder

The Pattern Wizard remembers your choice of custom pattern folder for future sessions. To choose a different folder, use the sfpref function. For example, to set the custom pattern folder toC:\patterns, enter:

sfpref(PatternWizardCustomDir=fullfile("C:","patterns"));

Alternatively, rename your existing custom pattern folder and do one of the following:

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 Pattern > 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.
    Input Description
    u 2-D matrix
    numrow Number of rows in the matrix
    numcol Number of columns in the matrix
  3. Right-click inside the function and select > . The function appears as an opaque box.
    A subcharted graphical function.
  4. Double-click the subcharted function to open it.
  5. Remove the default flow chart from the graphical function.
  6. On the Modeling tab, selectPattern > Custom. A dialog box opens, listing all the patterns that you have saved in your custom pattern folder.
    Dialog box that lists saved custom patterns.
  7. Select the upper triangle iterator pattern and clickOK. The Pattern Wizard adds your custom pattern to the graphical function.
  8. In the graphical function, in place of action1, substitute an appropriate action. This action repeats once for every row of the matrix.

MAB-Compliant Patterns from the Pattern Wizard

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

Decision Patterns

If

Flow chart that models an if statement.

If-Else

Flow chart that models an if else statement.

If-Elseif

Flow chart that models an if else if statement.

If-Elseif-Else

Flow chart that models an if else if else statement.

If-Elseif-Elseif-Else

Flow chart that models an if else if else if else statement.

Nested If

Flow chart that models a nested if statement.

Loop Patterns

For Loop

Flow chart that models a for loop.

While Loop

Flow chart that models a while loop.

DoWhile Loop

Flow chart that models a do while loop.

Switch Patterns

Two Cases

Flow chart that models a switch statement with two cases.

Three Cases

Flow chart that models a switch statement with three cases.

Four Cases

Flow chart that models a switch statement with four cases.

See Also

sfpref

Topics