How to fix while loop error when getting a mouse response in Psychtoolbox?

142 Views Asked by At

I want to get ratings from participants in Psychtoolbox in MATLAB by getting a mouse response; however when I do it, while loop does not allow me to get all responses and I can only get, say, 1 rating instead of 4 (or 4-5 ratings instead of 16). As far as I can understand while loop which I use to get a mouse response only get one rating from the word list, then closes and assumes that my answer is for all words. I could not solve the problem because honestly I don't understand the reason. The code works perfectly with a keyboard response (I can get 16 ratings from 4 adjectives x 4 words). Thus, I believe that I am missing something in while loop.

Here is a part of my code:

% Get the mouse value SetMouse(round(rand * screenXpixels), round(rand * screenYpixels), window);

for i=1:Shuffle(1:length(words)) for z=1:Shuffle(1:length(adjectives))

    while true    

    ...%There is written the codes to display adjectives and words

    [x, ~, buttons] = GetMouse(Window); 
    y=centerY; 
                
                x = x+1;
                x = min(x, screenXpixels);
                mc_pick = ceil(x/gridX);
                choice = 430-98*mc_pick; %choice 1-7

                % Do not let mouse to exceed rectangle
                if x < RectangleDimensions(1)+20 % if x coordinate of the mouse gets smaller than left border's x coordinate of green box
                    x=RectangleDimensions(1)+20; % keep it at left border's x coordinate
                elseif x > RectangleDimensions(3)-20 % if x coordinate of the mouse exceeds right border's x coordinate of green box
                    x=RectangleDimensions(3)-20; % keep it at right border's x coordinate
                end

                %Draw the vertical red box adjusted to the mouse
                %Its position changes according to mouse movements. It starts on the center since the mouse is set at the center. 
                %The Y axis for the red rectangle should not change according to the mouse
                %movements. It must stay at the center.
                Screen('FillRect', Window, red,[centerX-choice-centerX/51 3/2*centerY-30 centerX-choice+centerX/8 3/2*centerY+30], 4);
    
                Screen('Flip',Window);

              
                if  any(buttons)
                    break
                end
                
               
    end

        ...
        

end end

1

There are 1 best solutions below

1
DMR On

I think the issue is that you are checking for when a mouse is pressed, but you are then not waiting until the mouse button is released. In this case, when a person pressed a button, it might loop through several trials before the mouse button is released.

Here is a simplified example of what is happening:

% checking for a mouse press without checking for the release
% pressing a button will cycle through many trials before the button is released
disp('Press the Mouse Button');
for trial = 1:10
    fprintf('On Trial %i\n', trial);
    buttons = 0;
    while ~any(buttons)
        [~,~,buttons]=GetMouse;
        WaitSecs(0.001);
    end
end

Instead, wait until the mouse buton is released, before proceeeding to the next trial:

% checking for a mouse press and then check for the release
disp('Press the Mouse Button');
for trial = 1:10
    fprintf('On Trial %i\n', trial);
    buttons = 0;
    while ~any(buttons)
        [~,~,buttons]=GetMouse;
        WaitSecs(0.001);
    end
    
    % wait until all buttons released
    while any(buttons)
        [~,~,buttons]=GetMouse;
        WaitSecs(0.001);
    end
end

In the context of your original code example, change:

            if  any(buttons)
                break
            end

To:

if any(buttons)
    while any(buttons)
        [~,~,buttons]=GetMouse;
    end
    break
end

This will wait until the mouse buttons are released before breaking out of the while loop.