Psychtoolbox Not Screen Flipping in Data Acquisition Toolbox ScansAvailableFcn

Hello, I’m trying to get Psychtoolbox to provide live-input feedback to the screen using a NI USB-6002 DAQ. I’m using Psychtoolbox in conjunction with the Data Acquisition Toolbox’s background acquisition. This allows for continuous reading of data from the DAQ with the option of continuing Matlab function. Using the ScansAvailableFcn property, I can set a function to run after a preset number of scans have been acquired. Through this, I’ve been able to get Matlab to plot live data using it’s own graphical interface. However, when I try to put my PTB screen update code in the function, the screen will update once or twice and then just stop. No errors are sent to the screen, and the rest of the script doesn’t run.

I’ve tried adjusting how many scans that it waits for before Matlab calls the ScansAvailableFcn, but that hasn’t resulted in a different outcome.

Any idea what could be causing this or any advice on how to fix this?

Here is the function that I set as the ScansAvailableFcn to update the screen when new data is acquired:

function associationScreenUpdate(obj,evt,goal, u_limit, zero_limit, window, textXCoord, textYCoord, rectColor, centeredRegion, baseRect, centeredbaseRect, lineWidthPix, white)
    data = read(obj,obj.ScansAvailableFcnCount);
    b = double(data.Dev4_ai0(end)) - zero_limit;
    disp(b);

    %Code for establishing fill bar size
    assocForcePoints = round(b * 100 /u_limit);
        if assocForcePoints > 100
            assocForcePoints = 100;
        elseif assocForcePoints < 0
            assocForcePoints = 0;
        end
    forceHeight = baseRect(4)*assocForcePoints/100;

    %Determine if current hit or miss and set color of goal
    %area
    if (assocForcePoints <= goal+5) && (assocForcePoints >= goal-5)
      regionColor = [0 1 0];
    else
      regionColor = [1 0 0];
    end

    %Draw Rectangle and Text to Screen
    [normBoundsRect]= Screen('TextBounds', window, num2str(goal));
    goalText = CenterRectOnPoint(normBoundsRect, textXCoord, textYCoord);
    DrawFormattedText(window, num2str(goal), goalText(3), goalText(4), white)
    Screen('FrameRect', window, rectColor, centeredbaseRect, lineWidthPix);
    Screen('FillRect', window, rectColor, [centeredbaseRect(1) centeredbaseRect(2)+(baseRect(4)-forceHeight) centeredbaseRect(3:4)]);
    Screen('FillRect', window, regionColor, centeredRegion);
    Screen('Flip', window);
end

Psychtoolbox Version: 3.0.17 - Flavor: beta - Corresponds to SVN Revision 12153
Matlab Version: 9.10.0.1710957 (R2021a) Update 4
OS: Windows 7 Enterprise Service Pack 1 64-bit
Processor: Intel Core i5-4670 CPU @ 3.40 GHz
Installed Memory: 16.0 GB
Graphics Card: AMD Radeon R7 200 Series running with Adrenaline 2020 21.5.2 driver

Thank you much!

Grace
Chib Neuroeconomics Lab
Johns Hopkins University School of Medicine

Hello Grace,

what i can tell you is that PTB was not designed/implemented with use of Screen or other mex files from within such a Matlab callback in mind. It also wasn’t tested for compatibility with this approach. So this could be a Matlab bug, if 3rd party mex files are supposed to run normally from such a callback, but it could also be a PTB design limitation, given that we don’t know if some special constraints need to be taken into consideration for this to work. Mathworks docs don’t mention any special constraints.

Therefore I don’t know if this can be made to work the way you intend it, or if an alternative approach to your task would need to be taken. What i do know is that unless a volunteer comes up with a solution, this will probably take at least an hour of my time - probably more - to work on a diagnosis with you, which may or may not result in a fix or workaround or advice on alternative approaches. So you need to consult “help PsychPaidSupportAndServices” for how to get an hour of paid support for your issue. Also, due to various overlapping vacations at our company, you’d need to get your lab to buy a sufficient number of licenses (1 license == 1 hour of support at most) by end of tomorrow, Monday 9th August, otherwise you will likely have to wait until at least the 7th September for support - first come first served if multiple people need support at that time.

Btw. Windows-7 is no longer supported or tested with PTB 3.0.17, so i’d test if the problem persists on the latest Windows-10, otherwise you are basically on your own if things don’t work.

Best
-mario

This was my EXACT experience and I couldn’t find a solution. DAQ can use callbacks and draw live graphs, and I could use global variables to save the data since after it is read, it is removed from the buffer. The code worked in the incubator, but didn’t play well with PTB.

I am now experimenting in opening two PTB screens and plot using drawlines. However, I need to disable vertical synch for the experimenter monitor and keep it for the participant one. Since both screens are handled separate, this should work, but couldn’t find the right syntax for it. I registered here to ask this, but seeing this thread, I thought I’d try here first.

Goal: open two screens, skip sync test on one and keep it for the other.

What you want to do is not skip the sync test, but draw with vsync off. Thats the dontsync flag of screen(‘flip’ you need

But it will check for sync as soon as I open the window on that screen (and will never vertical sync, unfortunately) before it even gets to flip anything. Maybe I can skip the test before I open that one and then reinstate again, and use the dontsync flag when flipping.

One issue is that I tried dontsync in another context and it gave me a syntax error. Would you mind typing it out?

I don’t quite understand your problem, but if you open your subject screen fullscreen on monitor-0 with the sync testing enabled, then just open the experimenter screen windowed (not full-screen) on monitor-1 this works fine later when you flip with good temporal precision for the subject, and lazily (with dontsync) for the experimenter – at least on Linux it does. dontsync is the 4th parameter to pass when you flip.

http://psychtoolbox.org/docs/Screen-Flip

[VBLTimestamp StimulusOnsetTime FlipTimestamp Missed Beampos] = Screen(‘Flip’, windowPtr [, when] [, dontclear] [, dontsync] [, multiflip]);

An example from Dee’s Tobii toolbox, showing how to flip the subject and experimenter (operator) screens:


Going back to the OP — why not use two MATLAB processes?

1 Like

Hi @Ian-Max-Andolina, thanks for your reply. Could you be more detailed about how running two Matlab processes would help? Thank you!

As I understand the OP’s problem, they want to stream and plot data with a callback using data acquisition toolbox (DAT) and run a PTB loop — and the DAT callback is not triggering the PTB update properly. MATLAB fundamentally is not robustly multi-threaded/process — that is why you can’t just fire up a new thread, and things like the parallel processing toolbox exist (and Mario stated this scenario has not been explicitly developed for). Therefore it isn’t surprising the callback is not robust (and my intuition is the problem lies with DAT and MATLAB rather than PTB).

The simple solution is that you run MATLAB twice (the first MATLAB can automate running the second as you can run MATLAB via a command-line with code to execute automatically). Now each process can fully control itself and the OS is fully responsible for the multi-process part. The downside is if you need to share data between these (i.e. some sort of closed loop experiment where the data in may effect stimulus out) — then you need to use inter-process communication (IPC) to send messages and data back and forth (using something like the PTB pnet command)… The benefit of this is its flexibility, processes can run on one PC or two located anywhere on the same network, and run on different OS’es etc.

Another alternative is to just dump NI and the DAT, my limited experience is DAT and the NI drivers are far from optimised. Instead use something that would allow streamed data to be read as part of the PTB loop itself, or use a command-response mode as required…

Thank you much! That was very helpful!