Writing S-Functions | ![]() ![]() |
S-Function Outputs
An M-file returns an output vector containing the following elements:
sys
, a generic return argument. The values returned depend on the flag
value. For example, for flag = 3
, sys
contains the S-function outputs.
x0
, the initial state values (an empty vector if there are no states in the system). x0
is ignored, except when flag = 0
.
str
, reserved for future use. M-file S-functions must set this to the empty matrix, []
.
ts
, a two-column matrix containing the sample times and offsets of the block (see Specifying Sample Time in the online documentation for information on how to specify a block's sample time and offset).
ts
to [0 0]
. If you want your S-function to run at the same rate as the block to which it is connected (inherited sample time), set ts
to [-1 0]
. If you want it to run every 0.25
seconds (discrete sample time) starting at 0.1
seconds after the simulation start time, set ts
to [0.25 0.1]
.
You can create S-functions that do multiple tasks, each at a different sample rate (i.e., a multirate S-function). In this case, ts
should specify all the sample rates used by your S-function in ascending order by sample time. For example, suppose your S-function performs one task every 0.25 second starting from the simulation start time and another task every 1 second starting 0.1 second after the simulation start time. In this case, your S-function should set ts
equal to [.25 0; 1.0 .1]
. This will cause Simulink to execute the S-function at the following times: [0 0.1 0.25 0.5 0.75 1 1.1 ...]
. Your S-function must decide at every sample time which task to perform at that sample time.
You can also create an S-function that performs some tasks continuously (i.e., at every time step) and others at discrete intervals. See Example - Hybrid System S-Function) for an example of how to implement such a hybrid block.
![]() | S-Function Arguments | Defining S-Function Block Characteristics | ![]() |