%Batch file to analyze skin conductance data using Ledalab
%
% First Ledalab is used to compute the area under the phasic driver
% Then the results are selected for the correct response (trigger 1-6)


%Ledalab('L:\projects\jvbproject\Nicola Project CBN\CBNtrialdata_all_pp\GSR\','open','visionanalyzer','downsample',50,'smooth',{'adapt'},'analyze','CDA','export_era',[1 4 10 1],'overview',1)
%Ledalab('L:\projects\jvbproject\Nicola Project CBN\CBNtrialdata_all_pp\GSR\','export_era',[1 4 10 1])


clear all;

cd('L:\projects\jvbproject\Nicola Project CBN\CBNanalysisTrialdata\');

% load Ledalab analysis info
logdir1= 'L:\projects\jvbproject\Nicola Project CBN\CBNtrialdata_all_pp\GSR\era_14s\';

logstuff1 = dir( [ logdir1 '*_era.mat' ] );

for l=1:length(logstuff1)
    logstuffnames{l} = [ logdir1 logstuff1(l).name ];
    sjslisttotal{l} = logstuff1(l).name(1:12);
end

sjslist = unique(sjslisttotal);

% file for output
sjsFileTrials = ['CBN_GSR_trial_data_', date, '.txt']; %text file for analysis
fidTrials = fopen(sjsFileTrials, 'wt+');
all_gsrtrialdata_labels = ["Participant","ISCR","block","item"];
all_sjs_gsrtrialdata = [];
Pp_GSR = [];
pattern = '(CBN(\d+)([A-Z]+)(\d+))'; % necessary to isolate pp name


for s=1:length(sjslist) % loop through all subjects
    sjCode = cell2mat(sjslist(s));
    select_sjs_file = find(strcmp(sjslisttotal,sjslist(s))==1);
    for f=1:length(select_sjs_file) % loop throught all blocks
        file_id = select_sjs_file(f);
        block_file = load(logstuffnames{file_id});
        
        %select the SCR to the target word
        % data recorded in nS, but for analysis in muS, so divide by 1000.
        booltrigger =((block_file.results.Event.nid<7)&block_file.results.Event.nid>0);
        sj(s).gsr(f).trigger = block_file.results.Event.nid(booltrigger);
        %log the trial numbers
        booltrial =((block_file.results.Event.nid>9)&block_file.results.Event.nid<70);
        sj(s).gsr(f).trial = block_file.results.Event.nid(booltrial);
        numseg = length(sj(s).gsr(f).trial');
        %log the block number
        boolblock =((block_file.results.Event.nid>100)&block_file.results.Event.nid<119);
        sj(s).gsr(f).block = block_file.results.Event.nid(boolblock);
        
        % To correct for skewness take the log
        sj(s).gsr(f).logISCR = log(1+(block_file.results.CDA.ISCR(booltrigger))/1000);
        %sj(s).gsr(f).ISCR = (block_file.results.CDA.ISCR(booltrigger))/1000;
        
        % Save data in R-friendly format
        Pp(:,1)=repmat(regexp(sjCode, pattern, 'tokens'),numseg,1);
        GSR_pp = num2cell(sj(s).gsr(f).logISCR');
        %GSR_pp = num2cell(sj(s).gsr(f).ISCR');
        block = num2cell(repmat(sj(s).gsr(f).block,numseg,1));
        trial = num2cell(sj(s).gsr(f).trial');
        Pp_GSR_item_block = horzcat(Pp,GSR_pp,block, trial);
        Pp_GSR = vertcat(Pp_GSR,Pp_GSR_item_block);

        f=f+1;
        
    end
    %all_sjs_gsrtrialdata = vertcat(all_sjs_gsrtrialdata,Pp_GSR);
    
    s=s+1;
end
all_gsrtrialdata = vertcat(all_gsrtrialdata_labels,Pp_GSR);
writematrix(all_gsrtrialdata, sjsFileTrials);
fclose(fidTrials);
