User`s guide

Overview of the C MEX S-Function Routines
3-53
mdlTerminate
In the mdlTerminate routine, perform any actions that are necessary at the
termination of a simulation. For example, if memory was allocated in
mdlInitializeSizes or mdlStart,thisistheplacetofreeit.Supposeyour
S-function allocates a few chunks of memory and saves them in
PWork.The
following code fragment would free this memory:
static void mdlTerminate(SimStruct *S)
{
int i;
for (i = 0; i<ssGetNumPWork(S); i++) {
if (ssGetPWorkValue(S,i) != NULL) {
free(ssGetPWorkValue(S,i));
}
}
}
Using S-Functions With the Real-Time Workshop
In general, you can use S-functions in the Real-Time Workshop. However,
certain cases require these modifications to S-functions:
Setting names of extra modules used in building your S-function
Setting
RTWdata for the S-function
Adding an
mdlRTW function
S-Function Module Names for RTW Build’s
If your S-function is built with multiple modules, you must provide the build
process names of additional modules. You can do this through the Real-Time
Workshop template makefile technology, or more conveniently by using the
set_param MATLAB command. For example, if your S-function is built with
multiple modules, as in
mex sfun_main.c sfun_module1.c sfun_module2.c
then specify the names of the modules without the extension using the
command:
set_param(sfun_block,'SFunctionModules','sfun_module1 sfun_module2')