Welcome! Log In Create A New Profile

Advanced

Quit the task when the number of correct trials in each condition reaches a certain value

Posted by Yidong_YANG 
For the neural recording, we want to ensure that we can get enough trials for each condition.
Currently, I use a userloop function. First, I generated a condition file table that repeats each condition 20 times. Then, the condition is shuffled and traversed. When a trial isn't correct, it will be replicated and added at the end of the condition table. The task will end automatically if all the trials are finished. The function is below.

function [C,timingfile,userdefined_trialholder] = onlyFlash_userloop(MLConfig,TrialRecord)
    C = [];
    timingfile = 'onlyFlash.m';
    userdefined_trialholder = '';
    persistent timing_filenames_retrieved
    if isempty(timing_filenames_retrieved)
        timing_filenames_retrieved = true;
        return
    end
    
    persistent conditions
    locs = [9.6, 0; -9.6, 0];
    if isempty(conditions)
        ind_all = [1 2; 2 1];
        num_rep = 20;
        all_comb = repmat(ind_all, num_rep, 1);
        conditions = all_comb(randperm(size(all_comb,1)),:);
    end
    
    if isempty(TrialRecord.TrialErrors) % Start at block 1, trial 1
        trial_nb = 1;
    else
        trial_nb = TrialRecord.CurrentCondition + 1; % Linear incrementation of the trial until all of the trials of the current block have been done
        if TrialRecord.TrialErrors(end) ~= 0 % If the subject made an error, the same trial is repeated
            conditions = [conditions;conditions(TrialRecord.CurrentCondition,:)];
        elseif TrialRecord.CurrentCondition == length(conditions)
            TrialRecord.NextBlock = -1;
            timingfile = '';
            C = [];
            return
        end
    end

 
    target_num = conditions(trial_nb,1);
    distractor_num = conditions(trial_nb,2);

    
    C = {'crc(0.3, [1 1 1], 1, 0, 0)',...
        ['crc(1, [1 1 1], 0, ' num2str(locs(target_num,1)) ',' num2str(locs(target_num,2)), ')'],...
        ['crc(1, [1 0 0], 0, ' num2str(locs(target_num,1)) ',' num2str(locs(target_num,2)), ')'],...
        ['crc(1, [1 1 1], 0, ' num2str(locs(distractor_num,1)) ',' num2str(locs(distractor_num,2)), ')'],...
        'mov(central.mp4,0,0,1)'};
     
    TrialRecord.User.flash_object = target_num;
    TrialRecord.NextCondition = trial_nb;
end

However, if the subject keeps failing in one condition, he will do many repeated trials at the end. Now, we want to change the strategy. Instead of generating the condition table before, we want to randomly choose one condition to run for each trial. We track the number of correct trials for each condition, if all of them reach a certain value, then we finish the task. We have TrialRecord.TrialErrors to save the error codes of the past trials, but I didn't find a way to count the correct number for each condition.
Re: Quit the task when the number of correct trials in each condition reaches a certain value
November 14, 2023 09:47AM
There are so many different ways to program it. For the code you posted, I would count the number like this, for example.
tbl = tabulate(conditions(find(0==TrialRecord.TrialErrors),1))

You need to keep the history of played conditions somewhere, to do this calculation. You are using TrialRecord.NextCondition to store the next trial number redundantly, but that variable is supposed to indicate the next condition number (e.g., 1, when the target is on the right; 2, when it is on the left). Then you can track the condition history with TrialRecord.ConditionsPlayed.
https://monkeylogic.nimh.nih.gov/docs_TrialRecordStructure.html

The National Institute of Mental Health (NIMH) is part of the National Institutes of Health (NIH), a component of the U.S. Department of Health and Human Services.