Writing S-Functions    

S-Function Arguments

Simulink passes the following arguments to an S-function:

t
Current time
x
State vector
u
Input vector
flag
Integer value that indicates the task to be performed by the S-function

The following table describes the values that flag can assume and lists the corresponding S-function method for each value.

Table 2-1: Flag Argument
Flag
S-Function Routine
Description
0
mdlInitializeSizes
Defines basic S-Function block characteristics, including sample times, initial conditions of continuous and discrete states, and the sizes array.
1
mdlDerivatives
Calculates the derivatives of the continuous state variables.
2
mdlUpdate
Updates discrete states, sample times, and major time step requirements.
3
mdlOutputs
Calculates the outputs of the S-function.
4
mdlGetTimeOfNextVarHit
Calculates the time of the next hit in absolute time. This routine is used only when you specify a variable discrete-time sample time in mdlInitializeSizes.
9
mdlTerminate
Performs any necessary end-of-simulation tasks.


  Writing M S-Functions S-Function Outputs