How to capture time interval between two keypress.

I want to capture time between two keypress .Time when one key is released and next key is pressed.I made code using cputime.Its like
function Keyrelease_Test
h = figure;
set(h,'KeyReleaseFcn',@KeyUpCb,'KeyPressFcn',@KeyDownCb)
StartTime=0;
function KeyUpCb(~,~)
StartTime=cputime;
end
function KeyDownCb(~,~)
Time=cputime-StartTime;
disp(Time)
end
end
but when i press 3 key it gives output like: >> Keyrelease_Test 54.8968
0.1872
0
0.0156
but for 3 keypress it must give 2 time.please help me
i have also made code using now
function Keyrelease_Set
h = figure;
set(h,'KeyReleaseFcn',@KeyUpCb,'KeyPressFcn',@KeyDownCb)
t1=0;
t2=0;
function KeyUpCb(~,~)
t1=rem(now,1);
end
function KeyDownCb(~,~)
t2=rem(now,2);
Time=t2-t1;
disp(Time)
end
end
for 2 keypress it gives output like
Keyrelease_Set
0.8938
1.6852e-05
please help me with this code or i have to use any other function or logic??

 채택된 답변

Geoff Hayes
Geoff Hayes 2014년 6월 14일
In your first function, you are using the cputime command which, according to the documentation - type help cputime in the command window, returns the CPU time in seconds that has been used by the MATLAB process since MATLAB started. So this is not an appropriate function to use when measuring the difference between key presses and release.
In your second function, you are using the now command which, according to the documentation - type help now in the command window, returns the current date and time as a serial date number. So the values you are seeing are reasonable though are not in the expected seconds. Your use of rem(t1,1) returns the current time (though in the KeyDownCb you are using t2=rem(now,2) so you may wish to change the 2 to a 1). If you want to convert this to seconds, then you should apply the following conversion
function KeyDownCb(~,~)
DAYS_TO_SECS = 24*60*60;
t2=rem(now,1);
timeSecs=(t2-t1)*DAYS_TO_SECS;
disp(timeSecs)
end
The above conversion is used since the serial date number is a single number equal to the number of days since January 0, 0000 (see http://www.mathworks.com/help/matlab/matlab_prog/represent-date-and-times-in-MATLAB.html). Since you probably want seconds between key presses, then we just do the days to second conversion as 24 hours * 60 minutes * 60 seconds.
Try the above and see what happens.

댓글 수: 1

thanks for your help.i got my answer actually i am using wrong logic.thanks alot

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2014년 6월 14일

댓글:

2014년 6월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by