User`s guide

3 Writing S-Functions As C-MEX files
3-68
real_T *xD = ssGetRealDiscStates(S);
real_T *xC = ssGetContStates(S);
/* xD=xC */
if (ssIsSampleHit(S, 1, tid)) {
xD[0]=xC[0];
}
}
#define MDL_DERIVATIVES
/* Function: mdlDerivatives =================================================
* Abstract:
* xdot = U
*/
static void mdlDerivatives(SimStruct *S)
{
real_T *dx = ssGetdX(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
/* xdot=U */
dx[0]=U(0);
}
/* Function: mdlTerminate =====================================================
* Abstract:
* No termination needed, but we are required to have this routine.
*/
static void mdlTerminate(SimStruct *S)
{
}
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif
Example - Variable Step S-Function
The example S-function, vsfunc.c uses a variable step sample time. Variable
step-size functions require a call to
mdlGetTimeOfNextVarHit, which is an
S-function routine that calculates the time of the next sample hit. S-functions
that use the variable step sample time can only be used with variable step
solvers.
vsfunc is a discrete S-function that delays its first input by an amount
of time determined by the second input.
This example demonstrates how to correctly work with the fixed and variable
step solvers when the equations (functions) that are being integrated change