Main Content

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 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 nestedforloops in which the row indexiis always less than or equal to the column indexj. By using the Pattern Wizard, you can:

  1. 创建一个flow chart for the outer loop that iterates over the row indexi. SeeCreate Reusable Flow Charts.

  2. Extend the flow chart by inserting an inner loop that iterates over the column indexj. SeeInsert Logic Patterns in Existing Flow Charts.

  3. Save the flow chart as a custom pattern in the Pattern Wizard. SeeSave Custom Flow Chart Patterns.

  4. Reuse the custom pattern in a graphical function. SeeReuse Custom Flow Chart Patterns.

Create Reusable Flow Charts

To create a flow chart, on theModelingtab, select a pattern from thePatterngallery. Pattern selections include:

  • If,If-Else,If-Elseif, and other nested decision patterns.

  • For,While, andDoWhileloop patterns.

  • 开关模式4例。

  • Custom patterns that you saved for later reuse.

  • Patterns that you define in a MATLAB®.mfile.

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

For example, to create the outerforloop in the upper triangle iterator pattern:

  1. On theModelingtab, selectPattern>For循环.

  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. ClickOK. The Pattern Wizard generates this flow chart.

    Flow chart that models a for loop.

To complete the upper triangle iterator pattern, insert a secondforloop 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 thePatterngallery. 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 outerforloop pattern, select the vertical transition labeled{action1}.

  2. On theModelingtab, selectPattern>For循环.

  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 thati从来没有超过j. Also enter an action that retrieves each element in the upper triangle of the matrix.

    Pattern dialog box.

  4. ClickOK. 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.

插入指南逻辑模式

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 choose a pattern from thePatterngallery, 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 selectPattern>Save As Pattern.

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

  1. 创建一个folder for storing your custom patterns. SeeGuidelines for Creating a Custom Pattern Folder.

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

  3. On theModelingtab, 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 patternUpperTriangleIteratorand clickSave. The Pattern Wizard saves your pattern as a model fileUpperTriangleIterator.slxin 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, seeCreate 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.mdlor.slxextension.

Change Your Custom Pattern Folder

The Pattern Wizard remembers your choice of custom pattern folder for future sessions. To choose a different folder, use thesfpreffunction. 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:

  • 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 selectPattern>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 inDefine 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 selectGroup & Subchart>Subchart. 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 theModelingtab, 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 ofaction1, 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

If-Else

If-Elseif

If-Elseif-Else

If-Elseif-Elseif-Else

Nested If

Loop Patterns

For循环

While Loop

DoWhile Loop

Switch Patterns

Two Cases

Three Cases

Four Cases

See Also

Related Topics