Exit/terminate early the While Loop if "f" key is pressed.

How can I break out of a while loop I created with the key press "f"?
All I have managed at most to do is to make the loop wait for me to press the key "f" before continuing the rest of the loop, without breaking out of it. I'm trying to record mouse responses with the following code, and made it so that there needs to be a total of 30 mouse clicks recorded before ending the loop. However, I want there to be an option to break out earlier if the "f" key is pressed.
By the way, I am using Matlab along with Psychtoolbox.
This is the code:
csignal=zeros(1,30);
cnoise=zeros(1,30);
rec_coor = zeros(30,2);
counter =0;
timeStart = GetSecs();
Screen('BlendFunction',w1,GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
while(counter <30)
[c,x,y]=GetClicks(0,0);
coor=[x,y];
%Column1
if ((x>=85) & (x<=185))
if ((y>=70) & (y<=170))
Screen('FillRect',w1,[255,255,0,100],[85,70,185,170]);
csignal(1,1)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
elseif ((y>=240) & (y<=340))
Screen('FillRect',w1,[255,255,0,100],[85,240,185,340]);
csignal(1,28)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
elseif ((y>=410) & (y<=510))
Screen('FillRect',w1,[255,255,0,100],[85,410,185,510]);
cnoise(1,27)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
elseif ((y>=580) & (y<=680))
Screen('FillRect',w1,[255,255,0,100],[85,580,185,680]);
csignal(1,30)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
elseif ((y>=750) & (y<=850))
Screen('FillRect',w1,[255,255,0,100],[85,750,185,850]);
cnoise(1,7)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
elseif ((y>=920) & (y<=1020))
Screen('FillRect',w1,[255,255,0,100],[85,920,185,1020]);
cnoise(1,21)=1;
counter = counter+1;
rec_coor(counter,:) = coor;
end
end
%Column2
%Column3-10 (it keeps going to check all squares I drew on another script)
Screen('Flip',w1,[],1);
end
timeEnd = GetSecs();
RecallRT = (timeEnd - timeStart);

Answers (0)

Asked:

on 9 Mar 2017

Edited:

on 9 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!