Simulink Functions Overview
You can use Simulink® functions to define functions that are accessible in the Simulink model hierarchy. A Simulink function calculates a set of outputs based on the provided inputs and the implemented behavior of the function. You can use Simulink functions to:
Specify the function prototype which includes the function name, input and output arguments, for example,
y = f(u)
.Implement function behavior in the Simulink environment using a Simulink Function block, an exported function from a Stateflow® chart, or an S-Function block.
Call and reuse your function across the model hierarchy.
Specify Function Prototype
Similar to function signatures in MATLAB® and function headers in C++, Simulink functions use function prototypes to specify the input and output arguments and define how to interface with the function. Input and output arguments of a Simulink function can be signals within the model hierarchy or arguments specified local to the function. The function prototype is separate from the implementation of the function. Regardless of how or where you implement a function, the function caller uses the function prototype to invoke function execution. You can edit the function prototype by double-clicking the text displayed on the block.
For example, this function named myFilter
accepts an input
argument, inputSignal
, and returns filterSignal
as
an output argument.
filterSignal = myFilter(inputSignal)
To call this function, a function caller must send a signal through an input argument, execute the function, and return a signal through an output argument.
Implement Function Behavior
Each input and output argument specified in a function prototype must be represented in the implementation of the function. You can define and implement a Simulink function in several ways.
Simulink Function block — Define function using Simulink blocks.
Exported Stateflow function — Define graphical function, MATLAB function, or Simulink function within a Stateflow chart, and export to a Simulink model. For more information, see Export Stateflow Functions for Reuse (Stateflow).
S-Function block — Define function using an S-function block. See
ssDeclareSimulinkFunction
.
For more information, see Define a Simulink Function in a Model.
Call Simulink Functions
You can use a function caller and the function prototype to call and execute your function from anywhere in a model hierarchy. When a function is called, the caller sends data through input arguments to the function, and then receives data back from the function through output arguments. You can call a Simulink function in several ways.
Function Caller block — Call a function defined in Simulink or exported from Stateflow.
Chart (Stateflow) — Call a function defined in Simulink or exported from Stateflow.
MATLAB Function block — Call a function from a MATLAB language script.
MATLAB System block — Call a function using a System object™ and the MATLAB language.
S-Function block — Call a function using system methods. See
ssDeclareFunctionCaller
andssCallSimulinkFunction
.SimEvents® Event action – Call a function from an event action of a SimEvents block. See Events and Event Actions (SimEvents).
For more information, see Call a Simulink Function from a Model.
Determine Scope of Function
Function callers can only call functions that are within their scope. The scope of a function is defined by the relationship of the function caller to the subsystem or model containing the function definition. A function may be scoped, global, or port-scoped.
A scoped function is visible in its hierarchy. A function caller located at the same level as the function, or one or more levels below can refer to the function.
A global function is visible across a model hierarchy. A function caller located anywhere in the current model or in the parent model hierarchy can refer to the function.
A port-scoped function is visible only to a particular exporting function port in the current model. A function caller located outside of the current model can refer to the function through the exporting function port.
The scope of a function depends on how the function is implemented. A function defined in a Simulink Function block may be scoped, global, or port-scoped. For more information, see Scoped, Global, and Port-Scoped Simulink Function Blocks Overview. A function exported from a Stateflow chart is globally scoped. A function defined in an S-Function block may be scoped or global.
Configure Function Execution
A function may be executed synchronously or asynchronously.
Synchronous execution is when the caller invokes a function, the function runs immediately, and returns the output arguments to the caller.
Asynchronous execution is when the caller invokes a function, the function executes based on the priority order, and returns the output arguments to the caller.
You can view and edit the scheduling of asynchronous function calls by using the Schedule Editor.
The execution of a function may be specified as asynchronous in the block parameters of the function caller block.
Visualize and Trace Function Calls
Display Function Sequences
You can visualize Simulink function calls in the Sequence Viewer. The viewer shows when calls were made with the argument and the return values. For an example, see Monitor Ink Status on Shared Printer Using Simulink Functions.
Navigate to Related Blocks
You can select a Simulink Function or Function Caller block to highlight related blocks. If one or more of the related blocks are in a subsystem or referenced model, the related blocks and the Subsystem block or Model block that contains the related blocks are also highlighted.
To show a related block in an open diagram or new tab, pause on the ellipsis that appears after you select a Simulink Function or Function Caller block. Then, select Related Blocks from the action bar. When multiple blocks correspond to the selected block, a list of related blocks opens. You can filter the list by entering a search term in the text box. After you select a related block from the list, window focus goes to the open diagram or new tab that shows the related block.
Trace Simulink Functions
You can visually display connections between a Simulink function and their callers with function connectors. Simulink functions display function connectors at the top of the block containing the function definition. Function callers display function connectors at the bottom of the block calling the function. You can navigate from functions callers to the function by clicking the function link displayed at the bottom of the function caller.
To enable and disable tracing lines:
On the Debug tab, under Information Overlays , click Connectors.
Select the Function Connectors option from the Connectors pane that appears on the Simulink canvas.
If your model contains event blocks, you can use animation to
highlight function calls. To highlight and animate function calls, on the
Debug tab, in the Event Animation
section, set the animation speed to Slow
,
Medium
, or Fast
.
See Also
Simulink Function | MATLAB Function | Chart (Stateflow) | Function Caller