This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Reuse Simulink Components in Stateflow Charts

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

Simulink functions provide an efficient model design and improve readability by minimizing graphical and nongraphical objects. Typical applications include:

  • Defining a function that requires Simulink blocks, such as lookup tables (see About Lookup Table Blocks (Simulink))

  • Scheduling execution of multiple controllers

You can call Simulink functions defined inside of a Stateflow chart from the same chart. You can also call functions defined by a Simulink Function block in the model.

Where to Define a Simulink Function in a Chart

A Simulink function can reside anywhere in a chart, state, or subchart. The location of a function determines its scope, that is, the set of states and transitions that can call the function. Follow these guidelines:

  • If you want to call the function within only 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 that chart, put your Simulink function at the chart level.

Call a Simulink Function from Multiple Sites in a Chart

If you call a Simulink function from multiple sites in a chart, all call sites share the state of the function variables. For example, suppose you have a chart with two calls to the same Simulink function at each time step.

The function f contains a block diagram that increments a counter by 1 each time the function executes.

At each time step, the function f is called twice, which causes the counter to increment by 2. Because all call sites share the value of this counter, the data y and y1 increment by 2 at each time step.


This behavior also applies to external function-call subsystems in a Simulink model. For more information, see Using Function-Call Subsystems (Simulink).

Rules for Using Simulink Functions in Stateflow Charts

 Do not call Simulink functions in state during actions or transition conditions of continuous-time charts

 Do not call Simulink functions in default transitions if you enable execute-at-initialization mode

 Use only alphanumeric characters or underscores when naming input and output ports for a Simulink function

 Convert discontiguous signals to contiguous signals for Simulink functions

 Do not export Simulink functions

 Use the Stateflow Editor to rename a Simulink function

 Do not use Simulink functions in Moore charts

 Do not generate HDL code for Simulink functions

 Set properties of input ports explicitly for a Simulink function

 Verify that function-call expressions have inputs and outputs of correct size

 Pass arguments by value

See Also

Related Topics