Measure time until type a certain key

3 次查看(过去 30 天)
I am currently considering recording the time that an animal in a video performs a particular action.
I have tried using the tic toc function and the if syntax to "record the time until a specific key is pressed in the matrix when a specific key is pressed (e.g., Enter)," but it does not work.
I would appreciate any code suggestions you may have.

回答(2 个)

Simon Chan
Simon Chan 2023-3-27
Attached is a small program that you may try to see whether it is suitable for your purpose of not.
It will open an empty figure and trigger the callback when any key is pressed.
When the 'Enter' key is detected, it will log the current time and the time difference between 2 instances will be calculated.
function recordAction(src,event)
f = figure('WindowKeyPressFcn',@keyPress);
record = []; % Variable store the instance when 'Enter' key is pressed
%%
function keyPress(src,event)
key = get(src,'CurrentKey'); % Get the key pressed
if(strcmp (key , 'return'))
record = [record;datetime('now','Format','dd-MMM-yyyy HH:mm:ss.SSS')];
Delta =diff(record); % Calculate time difference
Delta.Format = 'hh:mm:ss.SSS'; % Set the duration format
display(Delta) % Display the time difference
end
end
end

Walter Roberson
Walter Roberson 2023-3-27
The time that something in video happens should be derived from the frame number and the frame rate that the action is detected on. If you are using a video reader you can typically inquire about the time property of the video -- which is usually the best thing to do as the time property properly takes into account variable frame rates.
If you want to determine the time that a human performs an action, then you should be considering looking at the third party Psychtoolbox http://psychtoolbox.org/ which offers tools for measuring reaction times for psychometric experiments.

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by