User`s guide

Introduction
1-7
Simulink makes repeated calls to S-functions in your model. During these
calls, Simulink calls S-function routines (also called methods), which perform
tasks required at e ach stage. These tasks include:
Initialization Prior to the first simulation loop, Simulink initializes the
S-function. During this s ta ge, Simulink:
- Initializes the
SimStruct, a simulation structure that contains
information about the S-function.
- Sets the number and size of input and output ports.
- Sets the block sample time(s).
- Allocates storage areas and the
sizes array.
Calculation of next sample hit If you’ve selected a variable step
integration routine, this stage calculates the time of the next variable hit,
that is, it calculates the next stepsize.
Calculation of outputs in the major time step After this call is complete,
all the output ports of the blo cks are valid for the current time step.
Updatediscretestates inthe majortime step In thiscall, allblocks should
perform once-per-time-step activities such a s updating discrete states for
next ti me around the simula tion loop.
Integration This applies to models with continuous states and/or
nonsampled zero crossings. If your S-function has continuous states,
Simulink calls theout put and derivative portions ofyourS-functio n at minor
time steps. This is so Simulink can compute the state(s) for your S-function.
If your S-function (C MEX only) ha s nonsampled zero crossings, t hen
Simulink will call the output and zero crossings portion ofyour S-function at
minor time steps, s o that it can locate the zero crossings.
Overview of M-File and C MEX S-Functions
In M-file S-functions, the S-function routines are implemented as M-file
subfunctions. In C MEX S-functions, they are implemented as C functions. All
the S-function routines available to M-file S-functions exist for C MEX
S-functions as well. However, Simulink provides a larger set of S-function
routines for C MEX S-functions. If an S-function routine exists for both M-file
and C MEX S-functions, its name is the same for both.
For an M-file S-function, Simulink passes a
flag parameter to the S-funct ion.
The
flag indicates the current simulation stage. You must write M-code that