Measure time until type a certain key

조회 수: 3 (최근 30일)
Shion
Shion 2023년 3월 27일
답변: Walter Roberson 2023년 3월 27일
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