| Contents | Index |
Yes
void mdlTerminate(SimStruct *S)
Terminate(s)
Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 MATLAB S-Function block.
This method performs any actions, such as freeing of memory, that must be performed when the simulation is terminated or when an S-Function block is destroyed (e.g., when it is deleted from a model).
In C MEX S-functions, the mdlTerminate method is called after either an Update Diagram or after a simulation for which one of the following conditions is met:
mdlStart has run and the S-function does not have any mdlInitializeConditions method defined.
mdlInitializeConditions has run.
In addition, if the SS_OPTION_CALL_TERMINATE_ON_EXIT option is set for a given S-function, and if mdlInitializeSizes is called, then the user is guaranteed that Simulink will call mdlTerminate. One reason to set the SS_OPTION_CALL_TERMINATE_ON_EXIT option is to allocate memory in mdlInitializeSizes rather than wait until mdlStart.
Note that Simulink calls mdlInitializeSizes under a number of circumstances, including compilation and simulation. Simulink will also call mdlInitializeSizes during model editing if you perform an operation such as the setting of parameters.
In C MEX S-functions, use the UNUSED_ARG macro if the mdlTerminate function does not perform any actions that require the SimStruct S to indicate that the S input argument is required, but not used in the body of the callback. To do this, insert the line
UNUSED_ARG(S)
after any declarations in mdTerminate.
Note When generating code for a noninlined C MEX S-function that contains this method, make sure the method is not wrapped in a #if defined(MATLAB_MEX_FILE) statement. For example: #if defined(MATLAB_MEX_FILE)
static void mdlTerminate(SimStruct *S)
{
/* Add mdlTerminate code here *
}
#endif The define statement makes the mdlTerminate method available only to a MATLAB MEX file. If the S-function is not inlined, the Simulink Coder product cannot use this method, resulting in link or run-time errors. |
Suppose your S-function allocates blocks of memory in mdlStart and saves pointers to the blocks in a PWork vector. The following code fragment would free this memory.
{
int i;
for (i = 0; i<ssGetNumPWork(S); i++) {
if (ssGetPWorkValue(S,i) != NULL) {
free(ssGetPWorkValue(S,i));
}
}
}
C, C++, MATLAB

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |