Clear Filters
Clear Filters

Psychtoolbox: Trying to create a vector that determines whether responses were correct or not

1 view (last 30 days)
I'm having issues with determining whether a response is correct on my experiment. For context my experiment requires the subject to make a choice about the number of dots after being presented with stimuli that requires using either the left or right arrow key for 10 trials. Whenever I check response_correct, it always shows up as NaN with no assigned value. Is there something I'm missing? Here's a snippet of the code:
response_correct = NaN (numTrials,1);
for i = 1:numTrials
rnd\_num = rand();
if rnd_num < 0.5
numDot = randi([1 50], 1, 1);
else
numDot = randi([50 100], 1, 1);
end
:::::::::::::::::::::::::::::::::::
dotts(i)= numDot;
answered = 1;
if (keyIsDown)
key = KbName(find(keyCode));
response_key {i} = key;
response_time(i) = time;
dotts(i)= numDot;
answered = 1;
if dotts(i) == randi([1 50], 1, 1) && strcmp(response_key{i},'k')==1
response_correct(i) = 1; % it's correct
elseif dotts(i) == randi([1 50], 1, 1) && strcmp(response_key{i},'d') ==1
response_correct(i) = 0;
elseif dotts(i) == randi([50 100], 1, 1) && strcmp(response_key{i},'k') ==1
response_correct(i) = 0;
elseif dotts(i) == randi([50 100], 1, 1) && strcmp(response_key{i},'d') ==1
response_correct(i) = 1;
end
results_response_correct = response_correct;
end

Answers (1)

Walter Roberson
Walter Roberson on 20 Feb 2022
Odd, I am certain that I showed someone how to solve this problem already. If I recall correctly, that person deleted their query and my response, which I understood to mean that they had solved the problem (perhaps with my help) and no longer needed to ask about it.
response_correct = NaN (numTrials,1);
for i = 1:numTrials
if rand() < 0.5
numDot = randi([1 50], 1, 1);
expected_key = 'k';
else
numDot = randi([50 100], 1, 1);
expected_key = 'd';
end
dotts(i) = numDot;
answered = false;
%at this point you should be either looping waiting for a response
%possibly with a timeout, or else asking psychtoolbox to read a key,
%possibly with a timeout
if (keyIsDown)
key = KbName(find(keyCode));
if ismember(key, {'k', 'd'})
response_key {i} = key;
response_time(i) = time;
answered = true;
if key == expected_key
response_correct(i) = 1; % it's correct
else
response_correct(i) = 0; % it is not correct
end
end
end
%at this point, answered == false if they did not press a key (yet), or
%if the key they pressed was not one of the two valid keys. You need to
%figure out what to do in either case. That might involve a timeout.
end

Community Treasure Hunt

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

Start Hunting!