how to code not responding as a correct answer?
이전 댓글 표시
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?
답변 (1개)
David Fink
2016년 7월 26일
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
카테고리
도움말 센터 및 File 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!