Register function to call when MEX function clears or MATLAB terminates
#include "mex.h" int mexAtExit(void (*ExitFcn)(void));
#include "fintrf.h" integer*4 mexAtExit(ExitFcn) subroutine ExitFcn()
Pointer to function you want to run on exit
mexAtExit to register a function to
call just before clearing the MEX function or terminating MATLAB®.
your MEX function a chance to perform tasks such as freeing persistent
memory and closing files. Other typical tasks include closing streams
Each MEX function can register only one active exit function at a time. If you call
mexAtExit more than once, then MATLAB uses the
ExitFcn from the more recent
mexAtExit call as the exit function.
If a MEX function is locked, then you cannot clear the MEX file. Therefore, if you attempt to
clear a locked MEX file, then MATLAB does not call the
In Fortran, declare the
the Fortran routine that calls
mexAtExit if it
is not within the scope of the file.
In C MEX files, throwing an exception in
MATLAB to crash.
See these examples in