Skip to Main Content Skip to Search
Product Documentation

mdlTerminate - Perform any actions required at termination of the simulation

Required

Yes

C Syntax

void mdlTerminate(SimStruct *S)

C Arguments

S

SimStruct representing an S-Function block.

MATLAB Syntax

Terminate(s)

MATLAB Arguments

s

Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 MATLAB S-Function block.

Description

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:

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.

C Example

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));
    }
  }
}

Languages

C, C++, MATLAB

See Also

ssSetOptions

  


Related Products & Applications

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