Main Content

Reuse Simulink Components in Stateflow Charts

A Simulink® function is a graphical object that enables you to call a Simulink subsystem in the actions of states and transitions. Simulink functions are supported only in Stateflow® charts in Simulink models.

Simulink functions can improve the efficiency of your design and increase the readability of your model. Typical applications include:

  • Defining a function that requires Simulink blocks

  • Scheduling execution of multiple controllers

A Simulink function can reside anywhere in a chart, state, or subchart. The location of a function determines the set of states and transitions that can call the function.

  • If you want to call the function within one state or subchart and its substates, put your Simulink function in that state or subchart. That function overrides any other functions of the same name in the parents and ancestors of that state or subchart.

  • If you want to call the function anywhere in a chart, put your Simulink function at the chart level.

  • If you want to call the function from any chart in your model, use a Simulink Function (Simulink) block to define the function directly in the Simulink canvas. For more information, see Simulink Functions Overview (Simulink).


A Simulink function cannot access chart or state data in the Stateflow hierarchy.

For example, this Simulink function has the name sim_fcn. It takes three arguments (a, b, and c) and returns two output values (x and y).

The function contains a Simulink subsystem that multiplies the first argument times a gain of 25 and combines the other two arguments into a complex output signal.

Define a Simulink Function

  1. In the object palette, click the Simulink function icon . Move your pointer to the location for the new Simulink function in your chart.

  2. Enter the signature label for the function.

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

    [return_val1,return_val2,...] = function_name(arg1,arg2,...)
    You can specify multiple return values and multiple input arguments. Each return value and input argument can be a scalar, vector, or matrix of values. For functions with only one return value, omit the brackets in the signature label.

    You must use unique variable names for all arguments and return values.

  3. To program the function, open the Simulink editor by double-clicking the function box. Initially, the editor contains a function-call Trigger (Simulink) block and Inport (Simulink) and Outport (Simulink) blocks that match the function signature. You cannot delete the Trigger block.

  4. In the Simulink editor, add blocks and connect them to the Inport and Outport blocks.

  5. Configure the Inport and Outport blocks.

    1. Double-click each block to open the Block Parameters dialog box.

    2. In the Signal Attributes tab, enter the Data type and Port dimensions of the input parameter or return value.

    3. Click OK.


    An Inport block in a Simulink function cannot inherit itsData type and Port dimensions. For more information, see Explicitly Set the Properties of Inport Blocks.

Call Simulink Functions in States and Transitions

You can call Simulink functions from the actions of any state or transition. You can also call Simulink functions from other functions.

To call a Simulink function, use the function signature and include an actual argument value for each formal argument in the function signature. If the data types of the actual and formal arguments differ, the function casts the actual argument to the type of the formal argument.

Specify Properties of Simulink Functions

You can specify properties for a Simulink function in the Model Explorer or the Block Parameters dialog box.

  • To use the Model Explorer:

    1. To open the Model Explorer, in the Modeling tab, select Model Explorer.

    2. In the Model Hierarchy pane, select the Simulink function.

    3. In the Block Parameters pane, edit the Simulink function properties.

  • To use the Block Parameters dialog box:

    1. In the Stateflow Editor, right-click the Simulink function.

    2. Select Properties.

    3. Edit the Simulink function properties.

For a description of the Simulink function properties, see Subsystem, Atomic Subsystem, CodeReuse Subsystem (Simulink).

You can specify additional properties of Simulink functions programmatically by using Stateflow.SLFunction objects. For more information about the Stateflow programmatic interface, see Overview of the Stateflow API.

Guidelines for Using Simulink Functions

Use Alphanumeric Characters and Underscores in Argument Names

This guideline ensures that the names of Inport and Output blocks are compatible with the identifier naming rules of Stateflow charts.

Explicitly Set the Properties of Inport Blocks

The Inport blocks in a Simulink function cannot inherit their data types and sizes. Therefore, you must set the Data type and Port dimensions of each Inport block that is not a scalar of type double.

The Outport blocks in a Simulink function can inherit sizes and data types based on the connections inside the subsystem. Therefore, you can specify the Data type and Port dimensions of these blocks as inherited.


To make it easier to update the properties of Inport blocks, you can specify data types and sizes as parameters.

Convert Discontiguous Signals to Contiguous Signals

Outport blocks in Simulink functions do not support discontiguous signals. If your function contains a block that outputs a discontiguous signal, insert a Signal Conversion (Simulink) block between the discontiguous output and the Outport block. This action ensures that the output signal is contiguous.

Blocks that can output a discontiguous signal include the Bus Creator (Simulink) block and the Mux (Simulink) block. For the Bus Creator block, the output is discontiguous when the block outputs a virtual bus. If you select Output as nonvirtual bus, the output signal is contiguous and no conversion is necessary.

Do Not Export Simulink Functions

Exporting a Simulink function results in a run-time error during simulation. To avoid this error, open the Chart properties dialog box and clear the Export Chart Level Functions check box.

Do Not Use Simulink Functions in Moore Charts

You cannot use Simulink functions in Moore charts. This restriction prevents violations of Moore semantics during chart execution.

Do Not Call Simulink Functions in Default Transitions That Execute During Chart Initialization

If you select the chart property Execute (enter) Chart At Initialization, you cannot call Simulink functions in default transitions that execute the first time that the chart awakens. Otherwise, the chart generates a run-time error during simulation.

Do Not Call Simulink Functions in State During Actions Or Transition Conditions of Continuous-time Charts

In continuous-time charts, you cannot call Simulink functions during minor time steps. Instead, call Simulink functions in actions that occur during major time steps: state entry or exit actions and transition actions. Calling Simulink functions in state during actions or transition conditions results in a run-time error during simulation.

Do Not Generate HDL Code for Simulink Functions

Simulink functions do not support HDL code generation. Generating HDL code for charts that contain Simulink functions results in a run-time error during simulation.

Pass Arguments by Value

Passing an argument to a Simulink function by reference results in a run-time error during simulation.

See Also

(Simulink) | (Simulink) | (Simulink) | (Simulink) | (Simulink) | (Simulink) | (Simulink)

Related Topics