User`s guide

Overview of the C MEX S-Function Routines
3-47
This code retrieves the FILE pointer from the pointer-work vector and passes it
to
fclose to close the file:
static void mdlTerminate(SimStruct *S)
{
if (ssGetPWork(S) != NULL) {
FILE *fPtr;
fPtr = (FILE *) ssGetPWorkValue(S,0);
if (fPtr != NULL) {
fclose(fPtr);
}
ssSetPWorkValue(S,0,NULL);
}
}
Note If you are using mdlSetWorkWidths, then any work vectors you use in
your S- function should be set to
DYNAMICALLY_SIZED in mdlInitializeSizes,
even if the exact value is known before
mdlIntializeSizes is called. The size
to be used by the S-function should be specified in mdlSetWorkWidths.
The synopsis is:
#define MDL_SET_WORK_WIDTHS /* Change to #undef to remove function. */
#if defined(MDL_SET_WORK_WIDTHS) && defined(MATLAB_MEX_FILE)
static void mdlSetWorkWidths(SimStruct *S)
{
}
#endif /* MDL_SET_WORK_WIDTHS */
For an example, see matlabroot/simulink/src/sfun_dynsize.c.
Memory Allocation
When creating an S-function, itis possiblethat theavailableworkvectors don’t
provide enough capability. In this case, you will need to allocate memory for
each instance of your S-function. The standard MATLAB API memory
allocation routines (
mxCalloc, mxFree) should not be used with C MEX
S-functions. The reason is that these routines are designed to be used with
MEX-files that are called from MATLAB and not Simulink. The correct
approach for allocating memory is to use the
stdlib.h (calloc, free) library