User`s guide
3 Writing S-Functions As C-MEX files
3-50
S-Function Routines Called During Simulation
Conceptually, the way Simulink performs a simulation is that it executes the
blocksin your modelin the order dictated by theirconnections.After executing
this list of blocks, the processisrepeated until the end of simulation is reached.
This repeated execution of the blocks is referred to as the simulation loop.
Your S-functions interact with the Simulink during simulations by the
S-function routines outlined in the simulation loop of the flowchart presented
on page 3-16.
mdlGetTimeOfNextVarHit
The optional routine, mdlGetTimeOfNextVarHit, to be used with variable step
solvers, is called to get the time of the next variable sample time hit. This
function is calledonce for every major integration time step. It must return the
time of the next hit by using
ssSetTNext. The time of the next hit must be
greater than
ssGetT(S).
Note that the time of the next hit can be a function of the input signal(s).
The synopsis is:
#define MDL_GET_TIME_OF_NEXT_VAR_HIT /* Change to #undef to remove function */
#if defined(MDL_GET_TIME_OF_NEXT_VAR_HIT) && \
(defined(MATLAB_MEX_FILE) || defined(NRT))
static void mdlGetTimeOfNextVarHit(SimStruct *S)
{
ssSetTNext(S, <timeOfNextHit>);
}
#endif /* MDL_GET_TIME_OF_NEXT_VAR_HIT */
For an example, see matlabroot/simulink/src/vsfunc.c.
mdlOutputs
In the mdlOutputs routine, you compute the outputs of your S-function block.
Generally outputs are placed in the output vector(s),
ssGetOutputPortSignal.
The synopsis is:
static void mdlOutputs(SimStruct *S, int_T tid)
{
} /* end mdlOutputs */
The tid (task ID) argument is used in conjunction with multirate S-functions
to determine when a particular task has a sample hit.