Often, especially during development of scripts and functions, I stop my MATLAB programs using Ctrl+C However, sometimes some things need to be handled even after this, such as closing file handles.
The messages displayed after pressing Ctrl+C look a lot like a regular stacktrace, but a try-catch statement does not catch it, so exception-safe handing is not enough. The onCleanup
functionality does work, but only in functions. The suggestion of using dbstop if error
is also not a proper solution as it requires manually resolving any issues.
Are there other ways to handle Ctrl+C?
Edit: Note that this is not the same question as the one linked in the comments, as I want to be able to execute some code to handle resources, instead of maintaining the data. Of course, maintaining the data allows for manual resource handling, but that is very inconvenient and will be very bad for my productivity during certain stages of development.