5

Is there a way to know when an MATLAB exits? I wanted to do some work, for example, release some resource, print some logs... I could add some code at some class's destructors. But since we cannot determine the order that MATLAB calls destructors, I am not sure which one is the last one so I can release the resource.

Can we register any callback to the MATLAB exit event if there exists such an event...?

Joe C
  • 2,629
  • 1
  • 22
  • 43

2 Answers2

8

There is no exit event I know of that's thrown when exiting from a function or MATLAB itself. However, there are two things you can do to handle final cleanup:

  1. Use onCleanUp objects: When exiting from a function, variables in the local workspace are destroyed (and exiting from MATLAB itself will destroy objects in the base workspace). When working with resources (files, etc.), good practice is to create an onCleanUp object to handle those resources in an exception-safe manner. This is discussed in more detail in the documentation and this question: How do you handle resources in MATLAB in an exception safe manner? (like “try … finally”)

  2. Create a finish.m file: When quitting MATLAB, it will look on the search path for a file named finish.m and run that code before terminating.

Community
  • 1
  • 1
gnovice
  • 124,379
  • 14
  • 254
  • 355
  • @RodyOldenhuis: Heh, would ya look at that. ;) – gnovice Nov 29 '16 at 20:16
  • ah, you deserve it. You took the effort to find better links :) – Rody Oldenhuis Nov 29 '16 at 20:16
  • Does MATLAB delete variables in the base space in the reverse order they got defined? – Joe C Dec 01 '16 at 21:28
  • 1
    @JoeC: I don't think there's a definitive order. The "Tips" section of the onCleanUp documentation says this: "If your program contains multiple cleanup objects, MATLAB does not guarantee the order that it destroys these objects. If the order of your cleanup functions matters, define one onCleanup object for all the tasks." – gnovice Dec 01 '16 at 21:36
  • @gnovice I hope an onCleanup object can be deleted after the deletion of the variables captured by the lambda of the onCleanup. Is this correct? – Joe C Dec 01 '16 at 21:59
  • 1
    @JoeC: Yes. Any variables used when creating a lambda/[anonymous function](https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html) will maintain the value those variables had when it was created. For example, this code will work just fine: `a = 1.3; b = .2; c = 30; parabola = @(x) a*x.^2 + b*x + c; clear a b c; x = 1; y = parabola(x);` – gnovice Dec 01 '16 at 22:11
  • Will onCleanup still run if MATLAB crashes? How should we release resources at crash? – Joe C Dec 02 '16 at 15:56
  • @JoeC: It probably depends on how and why MATLAB crashes. I don't know that there's any kind of guarantees or expectations about what will happen and when during any given crash. I would guess (pure speculation) you'd need some kind of system-level resource to help clean up after MATLAB itself crashes (killing zombie processes, etc.). – gnovice Dec 02 '16 at 21:30
6

You can place any cleanup actions in the finish.m file.

Similar to startup.m, MATLAB executes this file (when found on the MATLAB search path) just prior to program termination.

Also worth looking into is onCleanup. This simple class creates an object that, when destroyed, runs the function registered during object creation. This is extremely useful when dealing with files for example:

fid = fopen(filename, 'r');
OC = onCleanup(@() any(fopen('all')==fid) && fclose(fid));

% ...file reading and processing here
% ERROR HAPPENS HERE!
% ... more processing here

fclose(fid);

meaning, the file handle fid is still closed, even though the normal fclose(fid) was not reached. This is because the object OC was cleared implicitly by MATLAB after the error.

Rody Oldenhuis
  • 37,261
  • 7
  • 48
  • 95