function [dataPath, logPath] = sd_block(cfg,Gparams)

log = [];
log.cfg = cfg;

% Set up data and log paths
dataPath = [Gparams.DataPath, 'data_S', num2str(Gparams.subject_number),'_sd_block_', num2str(cfg.curBlock)];
while(exist([dataPath, '.mat'], 'file'))
    dataPath = [dataPath, '_1'];
end
dataPath = [dataPath, '.mat'];

logPath = [Gparams.LogPath, 'log_S', num2str(Gparams.subject_number),'_sd_block_', num2str(cfg.curBlock)];
while(exist([logPath, '.mat'], 'file'))
    logPath = [logPath, '_1'];
end
logPath = [logPath, '.mat'];

%% Trialmatrix

% Create a trial sequence, randomize trial order
trialmatrix = sd_trialmatrix(cfg,Gparams);

% log the trial matrix
log.trialmatrix = trialmatrix;

data            = cell(size(trialmatrix,2), 1);
log.trialinfo   = cell(size(trialmatrix,2), 1);
log.trialoutput = cell(size(trialmatrix,2), 1);

save(logPath, 'log');

%% Start presentation 

% Show fixation screen before first trial
Screen('DrawDots', Gparams.pWindow, [(Gparams.ScreenResX - 1)/2, (Gparams.ScreenResY-1)/2], Gparams.FixDotSize, 255, [], 1);
time = Screen('Flip', Gparams.pWindow);
WaitSecs('UntilTime', time + 2);

% loop over trials
for iTrial = 1:size(trialmatrix,2)

    funcfg                                  = [];
    funcfg.h_location                       = trialmatrix(1,iTrial);
    funcfg.stimulus_orientation             = trialmatrix(2,iTrial);
    funcfg.button_mapping                   = trialmatrix(3,iTrial);
    funcfg.curBlock                         = cfg.curBlock;
    funcfg.iTrial                           = iTrial;
    
    % save information about trial in log structure
    log.trialinfo{iTrial} = [ ...
        funcfg.h_location,...
        funcfg.stimulus_orientation ...
        funcfg.button_mapping];

    log.trialoutput{iTrial} = sd_trial(funcfg, Gparams);

    data{iTrial} = [ ...
        Gparams.subject_number, ...
        cfg.curBlock, ...
        iTrial, ...
        funcfg.h_location,...                               % horizontal location of the stimulus
        funcfg.stimulus_orientation,...                     % orientation of the stimulus
        funcfg.button_mapping,...                           % button mapping (1 = up is left and down is right, 2 = up is right and down is left)
        log.trialoutput{iTrial}.afc_response.button, ...    % button pressed (0 = down, 1 = up)
        log.trialoutput{iTrial}.afc_response.decision, ...  % decision about stimulus (0 = left, 1 = right)
        log.trialoutput{iTrial}.afc_response.rt];           % reaction time


    % save data
    save(dataPath, 'data');
    save(logPath, 'log');

    % Interrupt trial?
    [~, ~, keyCode] = KbCheck;
    if (ismember(KbName('F8'), find(keyCode)))
        DrawFormattedText(Gparams.pWindow, 'De onderzoeker heeft het expimeriment gepauzeerd.\n\n Even wachten a.j.b.', 'center', 'center', 255);
        Screen('Flip', Gparams.pWindow);

        while(KbCheck);
            WaitSecs(0.01);
        end;

        keyCode = 0;
        while(~ismember(KbName('F8'), find(keyCode)))
            [~, ~, keyCode] = KbCheck;
            WaitSecs(0.01);
        end

        Screen('Flip', Gparams.pWindow);

        WaitSecs(2);
    end

end

%% Feedback
data_mat = cell2mat(data);

nr_correct  = sum(data_mat(data_mat(:,5) == -Gparams.stim.sd_orientation,8) == 0) + sum(data_mat(data_mat(:,5) == Gparams.stim.sd_orientation,8) == 1) + sum(data_mat(:,5) == 0);
nr_tot      = sum(data_mat(:,5) == -Gparams.stim.sd_orientation) + sum(data_mat(:,5) == Gparams.stim.sd_orientation) + sum(data_mat(:,5) == 0);

msg = ['Dit blok heb je ' num2str(nr_correct) ' van de ' num2str(nr_tot) ' zebras goed gespot! \n\n\n' ...
    'Druk op een toets om door te gaan.'];

showtext(msg,Gparams)

end

