User`s guide

Overview of the C MEX S-Function Routines
3-51
For an example of an mdlOutputs routine that works with mult iple input and
output ports, see
matlabroot/simulink/src/sfun_multiport.c.
mdlUpdate
The optional routine mdlUpdate is called once for every major integration time
step. Discrete states are typically updated here, but this function is useful for
performing any tasks that should only take place once per integration step.
The
mdlUpdate routine is called i f you include this code in your S-function’s
main module:
#define MDL_UPDATE /* Change to #undef to remove function. */
#if defined(MDL_UPDATE)
static void mdlUpdate(SimStruct *S, int_T tid)
{
}
#endif /* MDL_UPDATE */
Use this code ifyour S-functionhas one ormore discretestates ordoes not have
direct feedthrough.
The reason forthis is that most S-functions that do not havediscrete states but
do have direct feedthrough do not have update functions. Therefore, Simulink
is able to eliminate the need for the extra call in these circumstances.
If your S -function needs to have its
mdlUpdate routine called and it does not
satisfy either of the above two conditions, specify that it has a discrete state
using the
ssSetNumDiscStates macro in the mdlInitializeSizes function.
The
tid (task ID) argument is used in conjunction with multirate S-functions
to determine when a particular task has a sample hit.
For an example, see
matlabroot/simulink/src/dsfunc.c.
mdlDerivatives
In the optional routine, mdlDerivatives, you compute the S-Function block’s
derivatives for its continuous states. The derivatives are placed in the
derivative vector,
ssGetdX(S). mdlDerivatives is called only during minor
time steps.
Each time the
mdlDerivatives routine is called it must explicitly set the value
of all derivatives. The derivative vector does not maintain the values from the