Clear Filters
Clear Filters

Measure time until type a certain key

2 views (last 30 days)
Shion on 27 Mar 2023
Answered: Walter Roberson on 27 Mar 2023
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.

Answers (2)

Simon Chan
Simon Chan on 27 Mar 2023
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

Walter Roberson
Walter Roberson on 27 Mar 2023
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 which offers tools for measuring reaction times for psychometric experiments.

Community Treasure Hunt

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

Start Hunting!