Let MATLAB to do something else during an animation while-loop

Dear all,

Is it possible to pre-load an animation (e.g. drifting squared wave gratings) so my MATLAB won’t be stuck in the while loop (see attached) during the duration of the animation? The reason I want to do this is that I need MATLAB to work on something else while the animation is playing. I imagine I can put the code for this “something else” in the animation “while” loop but that is going to be pretty bulky and I am afraid the 60-Hz refresh rate will limit the times my “something else” code is executed. I can also imagine using multithreading but that will require me to have the MATLAB Parallel Processing toolbox… Is there an elegant way to do this in PTB? Thanks!

Please see below for my animation “while” loop:

        t_start = GetSecs;
        while GetSecs < t_start + Par.StimDur
              phase = phase + d_phase;
              Screen('DrawTexture', Par.PTB.window, texDG, [], dstRect, ori, [], [], [], [], [], [phase, freq, contrast, 0]);
              Screen('Flip', Par.PTB.window);

Dear @mariokleiner and all, do you have any suggestions for this? Thank you!

MATLAB is simply not designed for multi-threaded operation. The parallel processing toolbox is designed for running algorithms over data in parallel, not really running separate threads. There is no concept of threads or all the infrastructure found in other languages. Therefore you need to get into fairly low-level hacks dealing with Java directly. A much simpler solution is to just run separate MATLAB processes. You can run many MATLAB instances together (running with --nodesktop so they don’t bring up lots of GUI). For communication between them you need to use some sort of inter-process communication (IPC). As PTB includes PNET, the easiest one is to use TCP/UDP to communicate between processes, with the advantage these can be either on the same machine or on different machines (I’ve done this so that my PTB loop can talk to online electrophysiological display on another computer).There are many other types of IPC, and ideally you should be able to pass MATLAB objects not only generic data between processes. This is all much less efficient than other languages that support lightweight threads (that are used for e.g. web servers that need thousands of threads to run concurrently), but we only need a few processes so I don’t think that is really an issue on most hardware…

1 Like

While Matlab or Octave is not designed for multi-threaded operation, Psychtoolbox can do some tasks in the background, e.g., flips via AsyncFlipBegin/End/Check, to make it possible to do things “in parallel” to visual stimulation in the main loop of the script. So many things can be done without the need for explicit multi-threading. Ditto stuff for response collection, audio i/o etc. So it is solvable without need for a 2nd Matlab/Octave process, depending on needs.

@bli You can search the forum for previous solutions to these problems. If you need targeted advice from me, please get your lab to buy priority support, as explained on our website or in the top-post of this forum, so i can devote some time to answering your question specifically.


Hi Ian, thanks so much for the tips on multiple MATLAB instances and the IPC! As what I need to do in parallel is very simple (response collection and audio), I’ll give Mario’s suggestion (AsyncFlipBegin/End/Check) a try. But good to know there’s the IPC option if I need to do something more complicated.

@mariokleiner Hi Mario, thanks so much for the AsyncFlipBegin/End/Check suggestion. I’ll give that a try. And I am happy to see the subscription options are out now! As a regular user, I’d be happy to support the PTB.

Great. If you only need parallel sound and response collection, PsychPortAudio allows to schedule things in parallel and KbQueues can be used for response collection in the background, so in these scenarios one usually does not even need async flips - these are the cases usually taken care of by PTB on the easy path.