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

log = [];
log.cfg = cfg;

% Set up data and log paths
dataPath = [Gparams.DataPath, 'data_S', num2str(Gparams.subject_number),'_tae_practice_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),'_tae_practice_block_', num2str(cfg.curBlock)];
while(exist([logPath, '.mat'], 'file'))
    logPath = [logPath, '_1'];
end
logPath = [logPath, '.mat'];

%% Trialmatrix

% set up the parts of the matrix

stim_levels         = repmat(Gparams.stim.tae_stim_levels,1,cfg.repetitions*2);

h_location          = zeros(1,cfg.repetitions*10);

adaptor_direction   = [ones(1,size(stim_levels,2)/4), -1*ones(1,size(stim_levels,2)/4), ... 
                       ones(1,size(stim_levels,2)/4), -1*ones(1,size(stim_levels,2)/4)];

% button_map          = round(rand)+1;
button_map          = 1;
button_mapping      = ones(1,size(stim_levels,2))*button_map;
                   
% join the parts of the matrix
% 1st column: horizontal location 
% 2nd column: orientation of stimulus
% 3rd column: direction of the adaptor
% 4th column: button mapping

trialmatrix = [h_location; stim_levels; adaptor_direction; button_mapping];

% randomize trial order
trialmatrix = trialmatrix(:,randperm(size(trialmatrix,2)));

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

save(logPath, 'log');

%% Start block

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

% Show button mapping for this block
Screen('DrawDots', Gparams.pWindow, [(Gparams.ScreenResX - 1)/2, (Gparams.ScreenResY-1)/2], Gparams.FixDotSize, 255, [], 1);
    if button_map == 1
        DrawFormattedText(Gparams.pWindow, '<            >', 'center', 'center', 255, [],[],[],[],[],[]);
    elseif button_map == 2
        DrawFormattedText(Gparams.pWindow, '>            <', 'center', 'center', 255, [],[],[],[],[],[]);
    end  
time = Screen('Flip', Gparams.pWindow);
WaitSecs('UntilTime', time + 5);
%}

% Show fixation cross before start block
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.adaptor_direction                = trialmatrix(3,iTrial);
    funcfg.button_mapping                   = trialmatrix(4,iTrial);

    % save information about trial in log structure
    log.trialinfo{iTrial} = [ ...
        funcfg.h_location, ...
        funcfg.stimulus_orientation, ...
        funcfg.adaptor_direction, ...
        funcfg.button_mapping];

    log.trialoutput{iTrial} = tae_practice_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.adaptor_direction, ...                       % direction of the adaptor
        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  = 2* (sum(data_mat(data_mat(:,5) == Gparams.stim.tae_stim_levels(1),9) == 0) + sum(data_mat(data_mat(:,5) == Gparams.stim.tae_stim_levels(5),9) == 1));
nr_tot      = 2* sum((data_mat(:,5) == Gparams.stim.tae_stim_levels(1)) + (data_mat(:,5) == Gparams.stim.tae_stim_levels(5)));

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

