PTB-ERROR: IMAGINGPIPE_FLIPTWHEN variable assignment failed on macOS (works on Windows)

The reproducer script and testing helped greatly, thanks!

So it appears that script hits a serious limitation of Matlabs interpreter itself, but apparently not a bug according to their docs, but something they define as “just how things are”. I beg to differ, and would call it a design flaw, but it is what it is. Details and workarounds here:

https://www.mathworks.com/help/matlab/matlab_prog/resolve-error-attempt-to-add-variable-to-a-static-workspace.html

The summary is that whenever you use a nested function, like the “cleanup” function in that script, embedded into a parent function, in this case “testpsycherror”, then the variable workspaces of these functions are now “static workspaces”, and no variables can be dynamically created by load ing a workspace or mat file, eval(), feval(), assignin() or by Mex files, the latter being what Psychtoolbox Screen() mex files does for various tasks like Apple Silicon macOS Support, very-high color precision framebuffers and some of the fine-grained VRR timing support on Linux, the Vulkan display backend for HDR / WCG display on all operating systems, VR/AR/MR eXtendedReality displays, but also support for special neuroscience stimulator devices like the stuff from VPixx or CRS, and a bunch of other advanced tasks involving the imaging pipeline.

The solution or rather workaround is as dumb as it is simple: One must either abstain from use of nested functions inside functions that call Screen('Flip') etc. or predefine all variables that Screen may use, as already defined variables can get assigned new values, if they are manually predefined.

If you add these statements to the top of ‘testpsycherror’, stuff works again:

IMAGINGPIPE_FLIPTWHEN=[];
IMAGINGPIPE_FLIPVBLSYNCLEVEL=[];

Or for that matter, if you see a similar error message in other contexts, also define the variables mentioned in the error message the same way. I guess I’ll add some help text to those error messages in the future, with tips for the workaround…

So far, so dumb.

for the original bug reporter, check your code for functions that call Screen('Flip') and also contain nested functions, and modify accordingly.

Wrt. Octave, Octave doesn’t have that limitations. In fairness though, I haven’t checked if this is still true, but in the past Octave had some other limitations / missing functionality in the area of nested functions wrt. Matlab, so sufficiently complex code relying on nested functions may not work at all or as intended with Octave for those reasons.

-mario