Waiting for specific time for clicking right or left button on mouse, then continue

Hello everybody, I have a problem with this following issue:
I generated some sounds in left or right ear randomly and participants should push the left or right button on mouse (it means if a sound has been presented in the left ear, left button should been pushed ). My problem is this: participants have just3 seconds for clicking and after that the script should continue. (I have 2 ways: one way if I use GetClicks in psychtoolbox it will pause (it should not be paused) and second way if I use GetMouse in psychtoolbox I can not define right or left button).
Please if anyone have any ideas tell me about that I really need it. Thanks in advance.

  1. First way: using from GetClicks causes pause
clc
clear;
SamplingRate=48000;
F=[250 500 1000 2000 4000];
t=linspace(0,1,SamplingRate);
ind=0;
memory{1,1}={0};
for i=1:length(F)
ind=1+ind;
Frequency = F(i);
y=sin(t*2*pi*Frequency);
sound(y,SamplingRate) ;
%% This here I want to wait 3 second for clicking, after that the script continues but it will stope by using GetClicks
[clicks,~,~,whichButton] = GetClicks; %% This function is related to the Psychtoolbox in Matlab
%%
pause(5)
memory{1,ind} = y';
end
  1. Second way: using GetMouse is good but I can not define right or left button in this code
clc
clear;
SamplingRate=48000;
wait4resTime = 3;
F=[250 500 1000 2000 4000];
t=linspace(0,1,SamplingRate);
ind=0;
memory{1,1}={0};
Z=[];
for i=1:length(F)
ind=1+ind;
Frequency = F(i);
y=sin(t*2*pi*Frequency);
    tStart=GetSecs;
sound(y,SamplingRate) ;
%% This here I want to wait 3 second for clicking, after that the script continues but I can not define the right or left button 
    MousePress=0; %initializes flag to indicate no response
    while    (MousePress==0 && ( (GetSecs-tStart) < wait4resTime-.1 )) 
        [x,y,buttons]=GetMouse();  %waits for a key-press
     MousePress=any(buttons);  %% I have problem and I can not define right or left button this here.
        Z(1,jnd)=MousePress;      
    end
        elapsedTime = GetSecs-tStart;
%%
pause(4)
memory{1,ind} = y';
end

Type in doc GetMouse to get the help for this function, specifically buttons will tell you which button was pressed.

http://psychtoolbox.org/docs/GetMouse

“buttons” is a 1xN matrix where N is the number of mouse buttons. Each element of the matrix represents one mouse button. The element is true (1) if the corresponding mouse button is pressed and false (0) otherwise

Instead of using any(buttons) you need to see which button is pressed. IIRC the order of buttons is platform specific.

How should I use button in this code?? Can you explain more about using button for finding which button has been pushed? I really need it and I do not know, which one that I should use, GetMouse or GetClicks in psychtoolbox???
I really stuck in this part.

whichButton = find(buttons);