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 block to define the function directly in the Simulink canvas. For more information, see Simulink Functions Overview.
Note
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.
In the object palette, click the Simulink function icon . Move your pointer to the location for the new Simulink function in your chart.
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 must use unique variable names for all arguments and return values.
To program the function, open the Simulink editor by double-clicking the function box. Initially, the editor contains a function-call Trigger block and Inport and Outport blocks that match the function signature. You cannot delete the Trigger block.
In the Simulink editor, add blocks and connect them to the Inport and Outport blocks.
Configure the Inport and Outport blocks.
Double-click each block to open the Block Parameters dialog box.
In the Signal Attributes tab, enter the Data type and Port dimensions of the input parameter or return value.
Click OK.
Note
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.
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.
You can set general properties for your Simulink function through its properties dialog box. To open the function properties dialog box, right-click the Simulink function box and select Properties. For a description of the function properties, see Subsystem, Atomic Subsystem, CodeReuse Subsystem.
This guideline ensures that the names of Inport and Output blocks are compatible with the identifier naming rules of Stateflow charts.
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.
Tip
To make it easier to update the properties of Inport blocks, you can specify data types and sizes as parameters.
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 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 block and the Mux 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.
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.
You cannot use Simulink functions in Moore charts. This restriction prevents violations of Moore semantics during chart execution.
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.
During
Actions Or Transition Conditions of Continuous-time ChartsIn 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.
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.
Passing an argument to a Simulink function by reference results in a run-time error during simulation.
Bus Creator | Inport | Mux | Outport | Signal Conversion | Simulink Function | Trigger