MATLAB: Parallel Problem: Get keyboard input in 60 sec, otherwise jump out

parallel computing

Hi to all, I recently encountered a problem. I want to get the keyboard input and read it and put something on the screen if the user puts a cerntain key such as 'q'. However, if the user doesn't put in anything in 20 sec, I want the time counter to start again. This procedure loops over for 60 times.
Can anyone help me with this? Thank you very much!

Best Answer

Shijia, you can do something like this using callbacks. Here's an example using figure callbacks. Save it as myFunction.m and see if you can follow what it does:
function myFunction
figure('KeyPressFcn',@kp);
ticCount = 0;
lastTic = 0;
startTimer()
function startTimer()
ticCount = ticCount+1;
if ticCount>10
fprintf('We''ve already reached the end!!!!\n')
return;
end
lastTic = tic;
fprintf('Starting the timer for time number %d\n',ticCount)
justWait(5)
end
function kp(~,evnt)
fprintf('User pressed %s\n',evnt.Character)
if evnt.Character == 'q'
fprintf('It took the user %g seconds...\n', toc(lastTic))
startTimer()
end
end
function justWait(len)
oldTicCount = ticCount;
fprintf('About to wait for %g seconds...\n',len)
pause(len)
if ticCount==oldTicCount
fprintf('Nothing happened for %g seconds...\n',len)
startTimer()
end
end
end
Related Question