Stateflow User's Guide Previous page   Next Page

Building a Simulink Model with a Stateflow Embedded MATLAB Function

Embedded MATLAB functions are special Stateflow functions that use the Embedded MATLAB Language, a rich subset of the MATLAB programming language. This section takes you through the steps of creating a Simulink model with a Stateflow block that calls an Embedded MATLAB function, stats. stats calculates a mean and a standard deviation for the values in vals and stores them in the Stateflow data mean and stdev, respectively. Use the following steps to create a model with a Stateflow diagram that calls an Embedded MATLAB function. In the process, learn how to use embedded MATLAB functions in Stateflow.

  1. Create a new Simulink model with the following blocks:

  2. Save the model as call_stats_function.
  3. In the Simulink model, double-click the Stateflow block to open its empty Stateflow diagram.

  4. In the Stateflow diagram editor, select the Embedded MATLAB Function tool

    and move the cursor into the empty diagram area to click and place a new Embedded MATLAB function.
  5. A text field with a flashing cursor appears in the middle of the new Embedded MATLAB function.
  6. Enter the label stats(vals) for the new Embedded MATLAB function, as shown.

  1. In the Stateflow diagram, draw a default transition into a terminating junction with the following condition action.
  1. The finished Stateflow diagram now has the following appearance:

    Like all Stateflow functions, the signature of an Embedded MATLAB function is entered in its label with the following syntax:

    Each argument and the return value can be a scalar, vector, or matrix of values. A matrix is a two-dimensional array of values. A vector is a matrix with a row or column dimension of 1. Multiple return values are not allowed.

  1. In the Stateflow diagram, double-click the function stats to edit its function body.
  2. In the Embedded MATLAB Editor, select the Explore/Define Data tool to open the Model Explorer.
  1. The Model Explorer window appears, as shown.

    Notice that the function stats is highlighted in the Model Hierarchy pane (left). The Contents pane (right) displays the argument data vals, a scalar of type double with a scope of Function Input that was added when you labeled the Embedded MATLAB function.

  1. Double-click the vals row under the Size column to change the size of vals to 4.
  1. Because vals is defined for an Embedded MATLAB function, its first index, which is not displayed, is 1 by default, as in MATLAB arrays.

  1. In the Model Explorer window, select the Chart level in the Model Hierarchy pane on the left and add the following data:

    Name
    Scope
    Size
    invals
    Input from Simulink
    4
    mean
    Output to Simulink
    Scalar (no change)
    stdev
    Output to Simulink
    Scalar (no change)
  1. You should now see the following data in the Model Explorer:

    Now that you have added the data invals, mean, and stdev to the Stateflow diagram Chart, notice that an input port for invals and output ports for mean and stdev appear on the Stateflow block in Simulink.

    Notice in the Model Explorer that the value of Port for mean is 1 and for stdev is 2. The value of Port orders the ports from the top to the bottom of the block for both input and output data. You can change the order of the ports by entering a new value for the Port field.

  1. Connect the Constant block and the Display blocks to the ports of the Stateflow block, as shown, and save the model (call_stats_function).


Previous page  Introduction to Embedded MATLAB Functions Programming a Stateflow Embedded MATLAB Function Next page

© 1994-2005 The MathWorks, Inc.