0

I created a MATLAB code using Psychtoolbox to make an experiment. Everything works as I intended but it seems the initial loading of the experiment takes too long. The task is a simple yes/no response task whether the target word('probe') appeared in the previous set of word stimuli.

I put basic intro text as an image and then wait for any keypress to start the experiment but it will take about 40 seconds to actually begin the first trial after any keystroke. I want to make it work without any delay. It should start its first trial immediately after any keystroke.

I checked the timestops with GetSecs() on numerous positions in the code and it was not anything to do with loading stimuli or initial setting of the experiment before the for loop I attached below.

To make things look simpler, I changed some of the variables into actual numbers I used. I can gurantee that it is not due to large stimuli size since it is only 1500 words. Once the for loop starts, it goes smoothly but it takes 40 seconds to actually start the first trial so I think it is something to do with a specific function in the for loop or the way I built it.

Please let me know if anything is too vague or unclear. I will do my best to make things read better.

Edit: I minimalized the code leaving only the function names used in Psychtoolbox. I left the functions I used in between loops to let you know if they could cause any delay. It will not be possible to run this without Psychtoolbox installed so I guess you can briefly examine the structure of the code.

for trial = 1:250
    for i = 1:6 
        DrawFormattedText();
        Screen();
        WaitSecs(0.5);
    end
    DrawFormattedText();      
    flipTime = Screen();
    WaitSecs(0.5);
    DrawFormattedText();
    flipTime = Screen();
    rt = 0;
    resp = 0;
        while GetSecs - flipTime < 3
            clear keyCode;
            RestrictKeysForKbCheck();
            [keyIsDown,secs,keyCode] = KbCheck;
            respTime = GetSecs;
            pressedKeys = find(keyCode);
            % ESC key quits the experiment
            if keyCode(KbName('ESCAPE')) == 1
                clear all
                close all
                sca
                return
            end
            % Check for response keys
            if ~isempty(pressedKeys)
                for i = 1:2
                    if KbName(i) == pressedKeys(1)
                        resp = i;
                        rt = respTime - flipTime;
                    end
                end
            end
            % Exit loop once a response is recorded
            if rt > 0
                break;
            end
        end
    if rt == 0 || rt > 3 % 3 second limit for subjects to react to the probe stimuli
       DrawFormattedText(); 
       Screen();
       WaitSecs(1);
    end
    Screen();
    vbl = Screen();
    WaitSecs(1);
    % Record the trial data into output data matrix
    respMat{1, trial} = trial;
    respMat{2, trial} = resp;
    respMat{3, trial} = rt;
end
KH Cheon
  • 13
  • 5
  • Welcome to StackOverflow! Please provide us with a [mcve], what you have provided so far cannot be run by us as there are undefined variables. Also by making a minimal example you can make your question appeal to a wider audience. You have already identified which parts are running slowly, make a short example with that and ask how to make that specific part run faster – Nicky Mattsson Sep 04 '18 at 08:25
  • I hope the changes I made make things better although I am not sure what would be the best way to present this kind of codes.. – KH Cheon Sep 04 '18 at 08:47
  • It would actually be easier to debug if you included the code as it's used in Psychtoolbox, i.e. instead of just "DrawFormattedText();", include all of the values used in the function call. Then others can try to run the same code on their machines. It's likely not your issue, but two things I noticed based on what you've posted so far, is that RestrictKeysForKbCheck() just needs to be called once per experiment, you're currently calling it once per trial, and that it is unnecessary to clear the keyCode variable on each trial because it is overwritten anyway after the call to KbCheck. – DMR Sep 05 '18 at 04:23
  • @DMR Thank you for such a valuable comment. I was actually expecting that kind of insights. I will make sure to use RestrictKeysForKbCheck() once. I will modify them and check the delay again. If the problem is not solved, I will edit the code posted above again such that you recommended – KH Cheon Sep 06 '18 at 06:08

0 Answers0