Detecting experiment closure from MATLAB window script

Christopher Stawarz's Avatar

Christopher Stawarz

23 Sep, 2015 03:49 PM

Hi Eghbal,

Offline, you asked: Is it possible for a MATLAB window script to be notified when an experiment is closed?

Unfortunately, it appears that this is not possible at present. When the experiment is closed, the MATLAB window resets itself, throwing away any accumulated events. This means that none of the events associated with the experiment closure are delivered to your MATLAB script, irrespective of the state of the synchronization variable at close time.

That said, I'm sure we can modify the MATLAB window to make this work. Let me think about the best way to do it.


  1. Christopher Stawarz closed this discussion on 11 Jul, 2016 06:01 PM.

  2. Support Staff 1 Posted by Christopher Sta... on 11 Oct, 2016 02:24 PM

    Christopher Stawarz's Avatar

    Hi Eghbal,

    I've implemented a solution to the problem of handling experiment closure from a MATLAB window script.

    The MATLAB window now supports "cleanup" functions. If a file named foo_cleanup.m exists in the same directory as foo.m, and the MATLAB window executes function foo at least once, then function foo_cleanup will be executed when the MATLAB window resets (i.e. when the current experiment is unloaded). It will be called with the last value returned by foo as its only argument. (In your case, I assume this will be a structure or array that contains open file handles that you want to close.)

    I've attached a copy of the MATLAB window demo that includes a cleanup function.

    This feature is in the current nightly build. When you have a chance, please try it out and let me know if it meets your needs.


  3. Christopher Stawarz closed this discussion on 30 Nov, 2016 03:46 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac