FAQ - How to close onscreen windows on errors?
Q: If my script aborts with an error, I'm left with a dead Psychtoolbox window that prevents me from accessing the Matlab command window. How can I close the onscreen window in case of such an error?
A: There are different approaches. If Screen detects an error inside a Psychtoolbox MEX subcommand, it will automatically close all open windows and other resources. If your error is in regular Matlab code, e.g., your own code, or if you aborted your script via the CTRL + C keypress combo, you are left with such a dead window. The simplest way to close the window is to type blindly the command sca. sca is a little M-File which will close all windows (it's a quick and dirty way to access Screen('CloseAll')), show the mouse cursor again if it was hidden etc. If you don't get a reaction to that command, it may be that your hidden Matlab command window doesn't have the keyboard input focus. Try pressing Apple key + Tab key on OS/X, or ALT key + Tab key on Linux and Windows one or multiple times and retry typing sca + Enter key. This should eventually refocus the hidden Matlab command window, so the sca commands is really entered into the Matlab window and takes effect. If all this doesn't help, try pressing CTRL + C a couple of times before the Tab key -- it may be that your script is still running in the background, so it needs to be interrupted first.
Other options of error handling or debugging:
- Use try catch end statements in your scripts, as demonstrated in many of our demos, but especially in ErrorCatchDemo. After such an error handling has happened, you can type the ple command to get a nice backtrace of the error, useful for debugging.
- During development and debugging of your code, don't use fullscreen onscreen windows, but provide the optional rect parameter to Screen, e.g., Screen('OpenWindow', screenId, [], [0 0 640 480]); would open an window on screen screenId in default background color white. The window however wouldn't cover the whole screen, but only the rectangular area with top-left corner (0,0) and bottom-right corner (640,480). This way, most of your desktop and Matlab window would be still visible and easily accessible for typing commands, reading output etc. Be aware though that visual stimulus onset timing and all reported visual onset timestamps may be unreliable or even wrong in windowed mode, so always test your final scripts timing in normal fullscreen mode. This is a limitation of the windowing systems of the current operating systems, it can't be addressed by us.
- Similarly, a dual display setup helps a lot in debugging. However, display timing may be affected by having a second display active which doesn't show onscreen windows of Psychtoolbox, but other application windows, Matlab windows or the Desktop. So only use for development, not for running your studies!
- If Matlab really doesn't respond to any of these measures any more due to some serious bug, you will need to kill it via the Windows taskmanager (CTRL+ALT+DEL key combo), Linux kill function (CTRL+ALT+ESC key combo, followed by a mouse click), or Mac OS/X Force quit function (ALT+Apple key+ESC). In that case you may want to use Matlabs diary command in a future Matlab session to store all Matlab output to a text file "diary.txt", so you can find out with a text editor what caused the severe crash of your script.
Good luck in debugging!