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.
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.
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.
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
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).