MATLAB Application Program Interface
  Go to function:
    Search    Help Desk 
mexAtExit    Examples   See Also

Register a function to be called when the MEX-file is cleared or when MATLAB terminates

C Syntax

Arguments

ExitFcn
  Pointer to function you wish to run on exit.

Returns

Always returns 0.

Description

Use mexAtExit to register a C function to be called just before the MEX-file is cleared or MATLAB is terminated. mexAtExit gives your MEX-file a chance to perform tasks such as freeing persistent memory and closing files. Typically, the named ExitFcn performs tasks like closing streams or sockets.

Each MEX-file can register only one active exit function at a time. If you call mexAtExit more than once, MATLAB uses the ExitFcn from the more recent mexAtExit call as the exit function.

If a MEX-file is locked, all attempts to clear the MEX-file will fail. Consequently, if a user attempts to clear a locked MEX-file, MATLAB does not call the ExitFcn.

Examples

Consider a MEX-file named WrtFile that calls mexAtExit to register an exit function. The first time WrtFile is called, WrtFile invokes the ANSI fopen routine to open a file named MyData for writing. Each time WrtFile is called, WrtFile writes the first five values of the input vector to MyData. When WrtFile is cleared, MATLAB automatically invokes ExitFcn, which calls fclose to close the stream to MyData.

Calling WrtFile three times writes three lines of data to MyData.

Clearing WrtFile causes the exit function to be called.

The contents of MyData are

For an additional example, see mexatexit.c in the mex subdirectory of the examples directory.

See Also

mexLock, mexUnlock



[ Previous | Help Desk | Next ]