Skip to Main Content Skip to Search
Product Documentation

How to Define a Simulink Function in a Stateflow Chart

Task 1: Add a Function to the Chart

Follow these steps to add a Simulink function to the chart:

  1. Click the Simulink function icon in the Stateflow Editor toolbar:

  2. Move your pointer to the location for the new Simulink function in your chart and click to insert the function box.

      Tip   You can also drag the function from the toolbar.

  3. Enter the function signature.

    The function signature specifies a name for your function and the formal names for the arguments and return values. A signature has this syntax:

    [r_1, r_2,..., r_n] = simfcn(a_1, a_2,..., a_n)

    where simfcn is the name of your function, a_1, a_2, ..., a_n are formal names for the arguments, and r_1, r_2, ..., r_n are formal names for the return values.

      Note   This syntax is the same as what you use for graphical functions, truth tables, and MATLAB functions. You can define arguments and return values as scalars, vectors, or matrices of any data type.

  4. Click outside the function box.

The following example shows a Simulink function that has the name sim_fcn, which takes three arguments (a, b, and c) and returns two values (x and y).

Task 2: Define the Subsystem Elements of the Simulink Function

Follow these steps to define the subsystem elements of the Simulink function:

  1. Double-click the Simulink function box.

    The contents of the subsystem appear: input and output ports that match the function signature and a single function-call trigger port.

  2. Add Simulink blocks to the subsystem.

  3. Connect the input and output ports to each block.

The following example shows the subsystem elements for a Simulink function.

Task 3: Configure the Function Inputs

Follow these steps to configure inputs for the Simulink function:

  1. Configure the input ports.

    1. Double-click an input port to open the Block Parameters dialog box.

    2. In the Signal Attributes pane, enter the size and data type.

      For example, you can specify a size of [2 3] for a 2-by-3 matrix and a data type of uint8.

  2. Click OK.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS