MATLAB: Can I define a callback in MATLAB 7.14 (R2012a) which is executed when the user shutsdown Windows

MATLAB

I want to be able to perform some clean-up or save some results in MATLAB and/or my MATLAB Compiler Standalone application when the user tries to shutdown Windows, is that possible?

Best Answer

It is possible to act upon a shutdown notification in MATLAB or a MATLAB Compiler Standalone by using the attached MEX-file.
The function needs to be compiled using one of the supported Microsoft compilers (LCC is not supported). And the function can then be called in the following ways:
status = shutdownCallback
Returns 0 when no callback is currently active or 1 when a callback as been defined.
shutdownCallback('Title','Reason',@Callback)
shutdownCallback('Title','Reason','Callback')
Define a new callback. The callback function can be defined as string or function handle.
shutdownCallback('delete')
Removes an existing callback if any.