| Writing S-Functions | ![]() |
An Example Involving a Pointer Work Vector
This example opens a file and stores the FILE pointer in the pointer-work vector.
The statement below, included in the mdlInititalizeSizes function, indicates that the pointer-work vector is to contain one element.
ssSetNumPWork(S, 1) /*pointer-work vector*/
The code below uses the pointer-work vector to store a FILE pointer, returned from the standard I/O function, fopen.
#define MDL_START /* Change to #undef to remove function. */ #if defined(MDL_START) static void mdlStart(real_T*x0, SimStruct*S) { FILE*fPtr; void **PWork = ssGetPWork(S); fPtr = fopen("file.data", "r"); PWork[0] = fPtr; } #endif /* MDL_START */
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); } }
#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.
| Work Vectors and Zero Crossings | Memory Allocation | ![]() |