how to code not responding as a correct answer?
Show older comments
For my experiment for certain stimuli I want participants to push the spacebar but for other stimuli I want participants to inhibit their responses. For this I want to be able to code that no keyboard response is correct or rather that a spacebar response to specific stimuli would be incorrect.
How would I go about this?
Answers (1)
David Fink
on 26 Jul 2016
If your application is in a figure, you can set a function that is called when a key is pressed. Then, since one of your responses is not to do anything, you will need to have a timer to determine when the next question should be displayed. You could use a global variable to store the user's response
Global variable in script/main function:
userResponse = 'nothing';
Whenever a key is pressed, call myFunction(key) (this code is written in script/main function):
fig = figure();
set(fig,'KeyPressFcn',@(fig,evt) myFunction(evt.Key));
In myFunction(key), you can check what key was pressed:
function myFunction(key)
if strcmp(key,'space')
userResponse = 'space';
else
userResponse = 'other key';
end
end
Declare a timer in the script/main function
questions = ('Press Space', 'Do nothing', ...);
responses = ('space', 'nothing', ...);
q = 0;
tim = timer('BusyMode','drop', ...
'ExecutionMode','fixedrate', ...
'Period',timer_period, ...
'TimerFcn',@myTimerFcn);
start(tim);
The timer function can check what the input was and advance the figure to the next question
function myTimerFcn(~,~)
if q > 0
if strcmp(responses(q),userResponse)
disp('Correct!');
else
disp('Incorrect :(');
end
end
% reset response
userResponse = 'nothing';
% go to next question
q = q + 1;
% Only keep going if there are more questions
if q <= numel(questions)
% display questions(q)
else
stop(tim);
% terminate program
end
end
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!