no correct reaction time or button press possible
44 views (last 30 days)
Show older comments
function [reactionTime, correct] = do_experiment(n, cond, target)
% This function creates one trial of the experiment
% n: trial number / whatever you use it for
% cond: condition for this trial
% target: 1 = target present (correct response: 't')
% 0 = target absent (correct response: 'n')
%% Create figure & show stimulus
Treisman_exp(n, cond, target)
% store figure handle
hFig = gcf;
% Reset any previously stored key
set(hFig, 'CurrentCharacter', char(0));
%% Measure reaction time.
tic;
aw = ''; % last key pressed
while ~ismember(aw, ['t', 'n'])
waitforbuttonpress;
aw = get(hFig, 'CurrentCharacter');
if ~ismember(aw, ['t', 'n'])
disp('Please press either ''t'' or ''n'' to respond.');
end
end
%waitforbuttonpress;
reactionTime = toc;
%% Check whether response is correct or not
if (target == 1 && aw == "t") || (target == 0 && aw == "n")
correct = 1;
else
correct = 0;
%disp('Please press either ''t'' or ''n'' to proceed');
end
pause(4);
% To close figure before opening next figure
if ishghandle(hFig)
close(hFig);
end
end
0 Comments
Answers (2)
dpb
on 18 Nov 2025 at 18:34
Edited: dpb
on 18 Nov 2025 at 22:00
You forgot to tell us precisely what symptoms you got -- what does "is possible" mean here?
Taking a guess, I expect the while loop isn't doing what you think and your code structure should be more like
...
%% Measure reaction time.
tic;
waitforbuttonpress;
aw = get(hFig, 'CurrentCharacter');
reactionTime = toc;
...
with all the error checking and so on later...
This will capture any keystroke, of course; it's then up to the error handling to validate the result and discard any invalid response.
It is a design decision for the experiment whether to time the length of time from start to when a user eventually enters a correct response or whether to start over if invalid key -- that isn't defined here although one can sorta' infer the former is what the while construct is trying to do. If that's the case, then you would need to put the waitforbuttonpress statement in a "do forever" loop, breaking out when the returned keystroke is acceptable and the final toc after that loop breaks.
Let's see if can do something like that here on the fly...
...
tic % set start
while true
waitforbuttonpress;
aw=hFig.CurrentCharacter;
if contains(aw, {'t', 'n'},'ignorecase',1) % at least give them case insensitive behavior
break
else
disp('Please press either ''t'' or ''n'' to respond.');
end
end
reactionTime = toc;
...
0 Comments
Walter Roberson
on 18 Nov 2025 at 19:14
Using tic/toc will not accurately measure reaction time.
I would recommend that you use methods from the third-party Psychtoolbox; it has extensive facilities for reaction time experiments.
1 Comment
dpb
on 18 Nov 2025 at 19:56
Edited: dpb
on 18 Nov 2025 at 21:40
On Windows, <Mathworks switched over to using the internal services> which should be about as good as going to get on the PC. I don't know much about the gettimeofday function for Linux/Mac; you certainly know more about Mac.
Not sure what the Psychtoolbox does internally, but I'd be surprised it would be able to do significantly better/different that would change any basic conclusions drawn. Then again... <vbg>
Since these really aren't very fast events as the fastest recorded response times to visual stimuli are in the 100+ msec range and the average is more like 250 msec, anything that can capture much more resolution than that is going to be adequate. The big issue would be the one about OS context switching and the like, and that's sorta' unavoidable and out of the measurement technique's control.
ADDENDUM
It would seem to me the biggest uncertainty in this kind of thing would be in setting a consistent start time from the presentation of the stimulus. In having some tools specifically for such details is where I could see a specialized toolbox in having some real advantage, probably.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!