User`s guide

4 Guidelines for Writing C MEX S-Functions
4-12
/*
* mdlInitializeSampleTimes - indicate that this S-function runs
*at the rate of the source (driving block)
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/*
* mdlOutputs - compute the outputs by calling my_alg, which
*resides in another module, my_alg.c
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
*y = my_alg(*uPtrs[0]);
}
/*
* mdlTerminate - called when the simulation is terminated.
*/
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
The S-function routine mdlOutputs contains a function call to my_alg,whichis
the C function that contains the algorithm that the S-function performs. This
isthecodefor
my_alg.c:
#include "tmwtypes.h"
real_T my_alg(real_T u)
{
return(u * 2.0);
}
The wrapper S-function (wrapsfcn) calls my_alg, which computes u * 2.0.To
build
wrapsfcn.mex, use the following command:
mex wrapsfcn.c my_alg.c
Place the call to
my_alg in
mdlOutputs.