function dop=dopSaveSum(dop)
% dopOSCCI Mark II: dopSaveSum~ 19-11-2010 (last edit)
%
% save summary information to data/text/tab delimitted file
%
% series of options/settings as to what should be saved:
%
fprintf('\n%s\n\t%s',['Running ',mfilename],...
    'Saving summary data:');
try
    if ~dop.sum.skip && ~dop.sum.quit && dop.def.saveData
        %% series of options about data to be saved
        dop.sav.saveLabels=0;
        dop.sav.data=[];
        if ~isfield(dop.sav,'LIoptions')
            fprintf('\n\t%s\n','First save - running setup:');
            dop.sav.saveLabels=1;
            dop.sav.LIoptions={'saveDescrip','save1sample','save2sample','saveRank',...
                'save1norm','save2norm'};
            dop.sav.extraOptions={'saveEpoch'}; % a list for the moment
            dop.sav.abb.eSel=dop.scrn.labels; %dop.def.saveEpochSelectionLabels;%{'all','scrn'};
            
            % list of data abbreviations - e.g.,
            % dop.li.stimOnset.scrnEpochs.all.*
            dop.sav.abb.saveDescrip=dop.def.saveDescripLabels;%{'LIepochs','LIpeakSec','LImean','LIstd','LIsem',...
            %'LImin','LImax','LImedian','LIiqr'};
            dop.sav.abb.save1sample=dop.def.save1sampleLabels;%{'LIt1sig','LIt1cohen'};
            dop.sav.abb.save2sample=dop.def.save2sampleLabels;%{'LIt2sig','LIt2cohen'};
            dop.sav.abb.saveRank=dop.def.saveRankLabels;%{'LIrsig','LIrZr'};
            
            dop.sav.abb.save1norm=dop.def.save1normLabels;%{'jb1_norm'};%'ks1_p','ks1_stat','ks1_ci'
            dop.sav.abb.save2norm=dop.def.save2normLabels;%{'jb2_Lnorm','jb2_Rnorm'}; %'ks2_Lp','ks2_Lstat','ks2_Lci','ks2_Rp','ks2_Rstat','ks2_Rci'
            
            dop.sav.abb.epoch=dop.def.saveEpochLILabels;%{'LImean','LIpeakSec'};%'LIpeakSample',
            
            dop.sav.fileName='dopIndividualSummaryData.dat';
            dop.sav.fileNameLoc=[dop.def.saveDir,dop.sav.fileName];
            
            dop.sav.sesLabel=''; % nothing if not running session summary
            % index options:
            % for epoch, base, & poi we have:
            % - mean, std, min, max, & rms
            % > this if for left, right, and difference.
            % then also, goodness of record
            % & LI variability for all trials, left, right, and difference.
            
            % >> just use definition information for the moment, don't need
            % extra variables.
            %         dop.sav.indices=dop.def.saveIndices; % mean, std, min, max, & rms % selection, saveIndexChs required also
            %         dop.sav.indexChs=dop.def.saveIndexChs; % on/off save left, right, difference indices
        else
            fprintf('\t%s\n', 'Save setup run previously.');
        end
        
        %% session?
        if dop.def.sesFileChk && dop.ses.ind==1
            % continue to saving session labels
            fprintf('\t%s\n','...continuing to save labels for session summary...');
            dop.sav.labels=[];
            dop.sav.saveLabels=1;
            dop.sav.sesLabel=['_S',num2str(dop.ses.session)]; % append this to labels
            if dop.ses.session==dop.ses.nSes+1 % final session
                dop.sav.sesLabel=['_Sall',num2str(dop.ses.nSes)]; % append this to labels
                dop.sav.abb.eSel=dop.scrn.labels; %dop.def.saveEpochSelectionLabels;%{'all','scrn'};
            end
        end
        %% Collect the data:
        %% > identification
        % start off with the doppler data file name
        if dop.sav.saveLabels
            dop.sav.labels{1}='fileName';
            if dop.def.grpFileChk
                dop.sav.labels{end+1}='group';
            end
            if dop.def.sesFileChk
                dop.sav.labels{end+1}='session';
            end
            if sum(dop.def.saveRejEpochs)
                for i = 1 :numel(dop.mk.evNames)
                    dop.tmp.en = dop.mk.evNames{i};
                    for j = 1 : numel(dop.def.saveRejEpochs)
                        if dop.def.saveRejEpochs(j) && sum(strcmp(dop.scrn.labels,dop.def.saveEpochSelectionLabels{j}))
                            dop.sav.labels{end+1} = ['epRej_',dop.tmp.en,'_',dop.def.saveEpochSelectionLabels{j}];
                        end
                    end
                end
            end
        end
        dop.sav.data{1}=dop.sum.fileName;
        if dop.def.grpFileChk
            dop.sav.data{end+1}=dop.sum.grp;
        end
        if dop.def.sesFileChk
            dop.sav.data{end+1}=dop.ses.session;
        end
        
        %% --------------------------------------------
        dop.tmp.ens=dop.mk.evNames; % event names
        
        %% --------------------------------------------
        %% > epoch rejection
        if sum(dop.def.saveRejEpochs)
            for i = 1 :numel(dop.mk.evNames)
                dop.tmp.en = dop.mk.evNames{i};
                for j = 1 : numel(dop.def.saveRejEpochs)
                    if dop.def.saveRejEpochs(j) && sum(strcmp(dop.scrn.labels,dop.def.saveEpochSelectionLabels{j}))
                        dop.sav.data{end+1} = dop.scrn.rej.(dop.tmp.en).(dop.def.saveEpochSelectionLabels{j});
                    end
                end
            end
        end
        
        %% --------------------------------------------
        %% > Index data - overall - not by trial... 11-11-10
        for i=1:length(dop.tmp.ens)
            dop.tmp.en=dop.tmp.ens{i}; % event name
            dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
            % not everything can be saved for each event type base and stim
            % only are restricted...
            % event type: whatever they might be
            
            if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                    if dop.scrn.saveData(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                        % index channels: left right difference
                        % FIXED Periods
                        for j=1:length(dop.def.saveIndexChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                            if dop.def.saveIndexChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                % filters/sections: epoch base poi
                                for jj=1:length(dop.def.saveIndexFilt); dop.tmp.inf=jj; % index filter
                                    if dop.def.saveIndexFilt(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                        % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                                        for jjj=1:length(dop.def.saveIndices); dop.tmp.ind=dop.def.saveIndices(jjj); % index
                                            if dop.tmp.ind; dop.tmp.indAbb=dop.def.saveIndexLabels{jjj}; dop.tmp.indVar=dop.def.saveIndexVarLabels{jjj};
                                                if dop.sav.saveLabels
                                                    % event, '_',epoch selection, ch, filt, index
                                                    dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en];
                                                end
                                                dop.sav.data{end+1}=...
                                                    dop.ind.(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.infAbb).(dop.tmp.indVar)(:,1,dop.tmp.ich);
                                                % this exists for each trial
                                            end
                                        end
                                    end
                                end
                            end
                        end
                        % > % Extras
                        for j=1:length(dop.def.saveIndexExtraChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                            if dop.def.saveIndexExtraChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                % filters/sections: epoch base poi
                                for jj=1:length(dop.def.saveIndexExtraChFilt); dop.tmp.chf=jj; % channel filter
                                    if dop.def.saveIndexExtraChFilt(dop.tmp.chf); dop.tmp.chfAbb=dop.def.saveIndexExtraChFiltLabels{dop.tmp.chf};
                                        for jjj=1:length(dop.def.saveIndexExtraTypeFilt); dop.tmp.typ=jjj;
                                            if dop.def.saveIndexExtraTypeFilt(dop.tmp.typ); dop.tmp.typAbb=dop.def.saveIndexExtraTypeFiltLabels{dop.tmp.typ};
                                                for jjjj=1:length(dop.def.saveIndicesExtra); dop.tmp.ind=jjjj;%dop.def.saveIndicesExtra(jjjj); % index
                                                    if dop.def.saveIndicesExtra(dop.tmp.ind); dop.tmp.indAbb=dop.def.saveIndexExtraLabels{dop.tmp.ind}; dop.tmp.indVar=dop.def.saveIndexExtraVarLabels{dop.tmp.ind};
                                                        dop.tmp.filtName=[dop.tmp.chfAbb,dop.tmp.typAbb];
                                                        if dop.sav.saveLabels
                                                            % event, '_',epoch selection, ch, filt, index
                                                            dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.typAbb,dop.tmp.chfAbb,'_',dop.tmp.indAbb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en];
                                                        end
                                                        dop.sav.data{end+1}=...
                                                            dop.ind.(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.filtName).(dop.tmp.indVar)(:,1,dop.tmp.ich);
                                                        % this exists for
                                                        % each trial
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        %% > Index by epoch
        if sum(dop.def.saveEpochInd) %dop.def.saveEpoch
            for i=1:length(dop.tmp.ens) % for each event
                dop.tmp.en=dop.tmp.ens{i}; % event name
                for j=1:numel(dop.def.saveEpochInd) %length(dop.def.saveIndices);
                    if dop.def.saveEpochInd(j);
                        dop.tmp.abb=dop.def.saveIndexLabels{j}; % index abbreviation
                        dop.tmp.indVar=dop.def.saveIndexVarLabels{j}; % variable name
                        if isfield(dop.ind.(dop.tmp.en),dop.scrn.labels{1}) % 'all'
                            for jj=1:numel(dop.def.saveIndexFiltLabels); % 'epoch','base','poi'
                                if dop.def.saveEpochIndFilter(jj);dop.tmp.fl=dop.def.saveIndexFiltLabels{jj}; % filter label
                                    for jjj=1:numel(dop.def.saveIndexChLabels);%(L R D)
                                        if dop.def.saveEpochIndCh(jjj);dop.tmp.cl=dop.def.saveIndexChLabels{jjj}; % channel label
                                            for jjjj=1:dop.def.epochs(i); dop.tmp.ep=jjjj; % epoch number %dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n; dop.tmp.ep=jjjj; % epoch number
                                                if dop.sav.saveLabels
                                                    dop.sav.labels{end+1}=[dop.tmp.cl,dop.tmp.fl,'_',dop.tmp.abb,'ep',num2str(dop.tmp.ep),'_',dop.tmp.en]; % could
                                                end
                                                if jjjj <= dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n
                                                    dop.sav.data{end+1}=...
                                                        dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).(dop.tmp.fl).(dop.tmp.indVar)(dop.tmp.ep,1,jjj);
                                                else
                                                    dop.sav.data{end+1} = 999;
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        %% --------------------------------------------
        %% > LI data
        %         dop.tmp.ens=dop.mk.evNames; % event names
        for i=1:length(dop.tmp.ens)
            dop.tmp.en=dop.tmp.ens{i}; % event name
            dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
            % not everything can be saved for each event type base and stim
            % only are restricted...
            if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                    if dop.scrn.saveData(ii); % dop.def.saveEpochSelection(ii) % saving particular set of data
                        dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                        for j=1:length(dop.sav.LIoptions)
                            dop.tmp.sop=dop.sav.LIoptions{j}; % save option
                            %                             if dop.def.(dop.tmp.sop)
                            for jj=1:length(dop.sav.abb.(dop.tmp.sop))
                                dop.tmp.abb=dop.sav.abb.(dop.tmp.sop){jj}; % data abbreviation
                                if dop.def.(dop.tmp.sop)(jj)
                                    if dop.sav.saveLabels
                                        dop.sav.labels{end+1}=[dop.tmp.abb,'_',dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en]; % could
                                    end
                                    dop.sav.data{end+1}=...
                                        dop.li.(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.abb);
                                end
                            end
                        end
                    end
                end
            end
        end
        %% > Split-Half LI data
        %         dop.tmp.ens=dop.mk.evNames; % event names
        if dop.def.splitCalcSave(2)
            for i=1:length(dop.tmp.ens)
                dop.tmp.en=dop.tmp.ens{i}; % event name
                dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
                % not everything can be saved for each event type base and stim
                % only are restricted...
                if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                    for iii=1:numel(dop.def.splitOptions)
                        if dop.def.splitOptions(iii);
                            for jjj=1:2%numel(dop.def.splitLabels{iii});
                                dop.tmp.sphl=dop.def.splitLabels{iii,jjj}; % split half label
                                for ii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                                    if dop.scrn.saveData(ii); % dop.def.saveEpochSelection(ii) % saving particular set of data
                                        dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                                        for j=1:length(dop.sav.LIoptions)
                                            dop.tmp.sop=dop.sav.LIoptions{j}; % save option
                                            for jj=1:numel(dop.def.([dop.tmp.sop,'Split']))
                                                if dop.def.([dop.tmp.sop,'Split'])(jj)
                                                    %                                                 for jj=1:length(dop.sav.abb.(dop.tmp.sop))
                                                    dop.tmp.abb=dop.sav.abb.(dop.tmp.sop){jj}; % data abbreviation
                                                    if dop.sav.saveLabels
                                                        dop.sav.labels{end+1}=['splitHalf',dop.tmp.sphl,'_',dop.tmp.abb,'_',dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en]; % could
                                                    end
                                                    dop.sav.data{end+1}=...
                                                        dop.sph.(dop.tmp.en).(dop.tmp.esel).(dop.tmp.sphl).all.(dop.tmp.abb); % all? 2-Jan-2011
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        %% > LI by epoch
        if sum(dop.def.saveEpochLI) %dop.def.saveEpoch
            for i=1:length(dop.tmp.ens) % for each event
                dop.tmp.en=dop.tmp.ens{i}; % event name
                for j=1:length(dop.sav.abb.epoch); dop.tmp.abb=dop.sav.abb.epoch{j};
                    if dop.def.saveEpochLI(j)
                        if isfield(dop.li.(dop.tmp.en),dop.scrn.labels{1})
                            for jj=1:dop.def.epochs(i); dop.tmp.ep=jj; % epoch number%dop.li.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n
                                if dop.sav.saveLabels
                                    dop.sav.labels{end+1}=[dop.tmp.abb,'_ep',num2str(dop.tmp.ep),'_',dop.tmp.en]; % could
                                end
                                if jj <= dop.li.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n
                                    dop.sav.data{end+1}=...
                                        dop.li.(dop.tmp.en).(dop.scrn.labels{1}).epoch.(dop.tmp.abb)(dop.tmp.ep);
                                else
                                    dop.sav.data{end+1} = 999;
                                end
                            end
                        end
                    end
                end
            end
        end
        %% --------------------------------------------
        %% Quality indices: Gor & Drop
        %% Drop
        for i=1:length(dop.tmp.ens)
            dop.tmp.en=dop.tmp.ens{i}; % event name
            dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
            % not everything can be saved for each event type base and stim
            % only are restricted...
            % event type: whatever they might be
            if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                for ii=1:numel(dop.def.saveEpochSelQual) %(dop.scrn.labels)%(dop.def.saveEpochSelection)
                    if dop.def.saveEpochSelQual(ii) && ~isempty(strmatch(dop.def.saveEpochSelectionLabels{ii},dop.scrn.labels)); dop.tmp.esel=dop.def.saveEpochSelectionLabels{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                        % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                        for jjj=1:numel(dop.def.saveDropInd); dop.tmp.ind=dop.def.saveDropInd(jjj); % index
                            if dop.tmp.ind; dop.tmp.indVar=dop.def.saveDropIndLabels{jjj}; dop.tmp.indAbb=['drop',dop.tmp.indVar,'_'];
                                % filters/sections: epoch base poi
                                for jj=1:numel(dop.def.saveDropFilt); dop.tmp.inf=jj; % index filter
                                    if dop.def.saveDropFilt(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                        % index channels: left right difference
                                        % FIXED Periods
                                        for j=1:numel(dop.def.saveDropChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                                            if dop.def.saveDropChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                                if dop.sav.saveLabels
                                                    % event, '_',epoch selection, ch, filt, index
                                                    dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en];
                                                end
                                                dop.sav.data{end+1}=...
                                                    dop.drop.(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.infAbb).(dop.tmp.indVar)(:,1,dop.tmp.ich);
                                                % this exists for each trial
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
                % epochs
                if dop.def.saveDropSelEp
                    for ii=1:numel(dop.def.saveEpochSelQual) %(dop.scrn.labels)%(dop.def.saveEpochSelection)
                        if dop.def.saveEpochSelQual(ii) && ~isempty(strmatch(dop.def.saveEpochSelectionLabels{ii},dop.scrn.labels)); dop.tmp.esel=dop.def.saveEpochSelectionLabels{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                            % index channels: left right difference
                            % FIXED Periods
                            for j=1:numel(dop.def.saveDropChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                                if dop.def.saveDropChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                    % filters/sections: epoch base poi
                                    for jj=1:numel(dop.def.saveDropFilt); dop.tmp.inf=jj; % index filter
                                        if dop.def.saveDropFilt(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                            % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                                            for jjj=1:numel(dop.def.saveDropInd); dop.tmp.ind=dop.def.saveDropInd(jjj); % index
                                                if dop.tmp.ind; dop.tmp.indVar=dop.def.saveDropIndLabels{jjj}; dop.tmp.indAbb=['drop',dop.tmp.indVar,'_'];
                                                    for jjjj=1:dop.def.epochs(i); dop.tmp.ep=jjjj; % epoch number %dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n; dop.tmp.ep=jjjj; % epoch number
                                                        if dop.sav.saveLabels
                                                            dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,'ep',num2str(dop.tmp.ep),dop.sav.sesLabel,'_',dop.tmp.en]; % could
                                                        end
                                                        if jjjj <= dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n
                                                            %                                                     dop.sav.data{end+1}=...
                                                            %                                                         dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).(dop.tmp.fl).(dop.tmp.indVar)(dop.tmp.ep,1,jjj);
                                                            dop.sav.data{end+1}=...
                                                                dop.drop.(dop.tmp.en).all.(dop.tmp.infAbb).(dop.tmp.indVar)(dop.tmp.ep,1,dop.tmp.ich);
                                                        else
                                                            dop.sav.data{end+1} = 999;
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        %% goodness of recording
        for i=1:length(dop.tmp.ens)
            dop.tmp.en=dop.tmp.ens{i}; % event name
            dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
            % not everything can be saved for each event type base and stim
            % only are restricted...
            % event type: whatever they might be
            if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                for ii=1:numel(dop.def.saveEpochSelQual) %(dop.scrn.labels)%(dop.def.saveEpochSelection)
                    if dop.def.saveEpochSelQual(ii) && ~isempty(strmatch(dop.def.saveEpochSelectionLabels{ii},dop.scrn.labels)); dop.tmp.esel=dop.def.saveEpochSelectionLabels{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                        % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                        for jjj=1:numel(dop.def.saveGorInd); dop.tmp.ind=dop.def.saveGorInd(jjj); % index
                            if dop.tmp.ind; dop.tmp.indVar=['gor',dop.def.saveGorIndLabels{dop.tmp.ind}]; dop.tmp.indAbb=[dop.tmp.indVar,'_'];
                                
                                % FIXED Periods
                                % filters/sections: epoch base poi
                                for jj=1:numel(dop.def.saveGorFilt); dop.tmp.inf=jj; % index filter
                                    if dop.def.saveGorFilt(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                        % index channels: left right difference
                                        for j=1:numel(dop.def.saveGorChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                                            if dop.def.saveGorChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                                if dop.sav.saveLabels
                                                    % event, '_',epoch selection, ch, filt, index
                                                    dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en];
                                                end
                                                dop.sav.data{end+1}=...
                                                    dop.ind.(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.infAbb).(dop.tmp.indVar)(:,1,dop.tmp.ich);
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
                % epochs
                %                 if dop.def.saveGorSelEp
                %                     for ii=1:numel(dop.def.saveEpochSelQual) %(dop.scrn.labels)%(dop.def.saveEpochSelection)
                %                         if dop.def.saveEpochSelQual(ii) && ~isempty(strmatch(dop.def.saveEpochSelectionLabels{ii},dop.scrn.labels)); dop.tmp.esel=dop.def.saveEpochSelectionLabels{ii}; % epoch selection all trials or screened % dop.def.saveEpochSelection(ii); dop.tmp.esel=dop.sav.abb.eSel{ii}; % epoch selection all trials or screened
                %                             % index channels: left right difference
                %                             % FIXED Periods
                %                             for j=1:numel(dop.def.saveGorChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                %                                 if dop.def.saveGorChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                %                                     % filters/sections: epoch base poi
                %                                     for jj=1:numel(dop.def.saveGorFilt); dop.tmp.inf=jj; % index filter
                %                                         if dop.def.saveGorFilt(dop.tmp.inf); dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                %                                             % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                %                                             for jjj=1:numel(dop.def.saveGorInd); dop.tmp.ind=dop.def.saveGorInd(jjj); % index
                %                                                 if dop.tmp.ind; dop.tmp.indVar=['gor',dop.def.saveGorIndLabels{jjj}]; dop.tmp.indAbb=[dop.tmp.indVar,'_'];
                %                                                     for jjjj=1:dop.def.epochs(i); dop.tmp.ep=jjjj; % epoch number %dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n; dop.tmp.ep=jjjj; % epoch number
                %                                                         if dop.sav.saveLabels
                %                                                             dop.sav.labels{end+1}=[dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,'ep',num2str(dop.tmp.ep),dop.sav.sesLabel,'_',dop.tmp.en]; % could
                %                                                         end
                %                                                         if jjjj <= dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).epoch.n
                %                                                             %                                                     dop.sav.data{end+1}=...
                %                                                             %                                                         dop.ind.(dop.tmp.en).(dop.scrn.labels{1}).(dop.tmp.fl).(dop.tmp.indVar)(dop.tmp.ep,1,jjj);
                %                                                             dop.sav.data{end+1}=...
                %                                                                 dop.ind.(dop.tmp.en).all.(dop.tmp.infAbb).(dop.tmp.indVar)(dop.tmp.ep,1,dop.tmp.ich);
                %                                                         else
                %                                                             dop.sav.data{end+1} = 999;
                %                                                         end
                %                                                     end
                %                                                 end
                %                                             end
                %                                         end
                %                                     end
                %                                 end
                %                             end
                %                         end
                %                     end
                %                 end
            end
        end
        %% --------------------------------------------
        %% behavioural
        %% > Index data - overall - not by trial... 11-11-10
        if dop.def.behFileChk && dop.def.saveBehData
            %             dop.tmp.ens=dop.mk.evNames; % event names
            for i=1:length(dop.tmp.ens)
                dop.tmp.en=dop.tmp.ens{i}; % event name
                dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
                % not everything can be saved for each event type base and stim
                % only are restricted...
                % event type: whatever they might be
                if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                    for ii=1:length(dop.beh.li.behNames); dop.tmp.bn=dop.beh.li.behNames{ii}; % behaviour name
                        for iii=1:length(dop.beh.li.(dop.tmp.bn));dop.tmp.blv=dop.beh.levels.(dop.tmp.bn)(iii); % behaviour level
                            % dop.beh.li.(dop.tmp.bn)(dop.tmp.bl).eventName...
                            for iiii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                                if dop.scrn.saveData(iiii); dop.tmp.esel=dop.sav.abb.eSel{iiii}; % epoch selection all trials or screened
                                    %                                 if dop.def.saveEpochSelection(iiii); dop.tmp.esel=dop.sav.abb.eSel{iiii}; % epoch selection all trials or screened
                                    % index channels: left right difference
                                    for j=1:length(dop.def.saveIndexChs); dop.tmp.ich=j;%dop.def.saveIndexChs(j); % index channel
                                        if dop.def.saveIndexChs(dop.tmp.ich); dop.tmp.chAbb=dop.def.saveIndexChLabels{j};
                                            % filters/sections: epoch base poi
                                            for jj=1:length(dop.def.saveIndexFilt); dop.tmp.inf=jj; % index filter
                                                if dop.tmp.inf; dop.tmp.infAbb=dop.def.saveIndexFiltLabels{jj};
                                                    % indices = mean, std, min, max, & rms % selection, saveIndexChs required also
                                                    for jjj=1:length(dop.def.saveIndices); dop.tmp.ind=dop.def.saveIndices(jjj); % index
                                                        if dop.tmp.ind; dop.tmp.indAbb=dop.def.saveIndexLabels{jjj}; dop.tmp.indVar=dop.def.saveIndexVarLabels{jjj};
                                                            if dop.sav.saveLabels
                                                                % event, '_',epoch selection, ch, filt, index
                                                                dop.sav.labels{end+1}=[...
                                                                    dop.tmp.bn,num2str(dop.tmp.blv),'Beh_',...
                                                                    dop.tmp.chAbb,dop.tmp.infAbb,'_',dop.tmp.indAbb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en]; % could
                                                            end
                                                            dop.sav.data{end+1}=...
                                                                dop.beh.ind.(dop.tmp.bn)(iii).(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.infAbb).(dop.tmp.indVar)(:,1,dop.tmp.ich);
                                                            % this exists for each trial
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
            
            %% --------------------------------------------
            %% > beh LI data
            %             dop.tmp.ens=dop.mk.evNames; % event names
            for i=1:length(dop.tmp.ens)
                dop.tmp.en=dop.tmp.ens{i}; % event name
                dop.tmp.evType=dop.def.evType(i); % event type: 0 = base, 1 = stim only, 2 = both
                % not everything can be saved for each event type base and stim
                % only are restricted...
                if dop.tmp.evType % don't save base info - it's all in stim only - provided it's matched...
                    for ii=1:length(dop.beh.li.behNames); dop.tmp.bn=dop.beh.li.behNames{ii}; % behaviour name
                        for iii=1:length(dop.beh.li.(dop.tmp.bn));dop.tmp.bl=iii; % behaviour level
                            % dop.beh.li.(dop.tmp.bn)(dop.tmp.bl).eventName
                            % ...
                            for iiii=1:length(dop.scrn.labels)%(dop.def.saveEpochSelection)
                                if dop.scrn.saveData(iiii); %dop.def.saveEpochSelection(iiii) % saving particular set of data
                                    dop.tmp.esel=dop.sav.abb.eSel{iiii}; % epoch selection all trials or screened
                                    for j=1:length(dop.sav.LIoptions)
                                        dop.tmp.sop=dop.sav.LIoptions{j}; % save option
                                        
                                        for jj=1:numel(dop.def.(dop.tmp.sop)); %length(dop.sav.abb.(dop.tmp.sop))
                                            if dop.def.(dop.tmp.sop)(jj)
                                                dop.tmp.abb=dop.sav.abb.(dop.tmp.sop){jj}; % data abbreviation
                                                if dop.sav.saveLabels
                                                    dop.sav.labels{end+1}=[...
                                                        dop.tmp.bn,num2str(dop.beh.levels.(dop.tmp.bn)(dop.tmp.bl)),'beh_',...
                                                        '_',dop.tmp.abb,dop.tmp.esel,dop.sav.sesLabel,'_',dop.tmp.en]; % could
                                                end
                                                dop.sav.data{end+1}=999;
                                                if isfield(dop.beh.li.(dop.tmp.bn)(iii).(dop.tmp.en).(dop.tmp.esel).all,dop.tmp.abb)
                                                    dop.sav.data{end}=...
                                                        dop.beh.li.(dop.tmp.bn)(iii).(dop.tmp.en).(dop.tmp.esel).all.(dop.tmp.abb);
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
        % end behavioural
        
        %% Save the data:
        %% > labels
        if dop.sav.saveLabels
            %             if ~exist(dop.sav.fileNameLoc,'file')
            %                 dop.tmp.fid=fopen(dop.sav.fileNameLoc,'w'); % open the file to write
            %             else
            dop.tmp.fid=fopen(dop.sav.fileNameLoc,'a+'); % open the file to write
            %             end
            for i=1:length(dop.sav.labels) % write each label
                %                 dop.sav.labels{i}
                fprintf(dop.tmp.fid,'%s\t',dop.sav.labels{i});
            end
            if or(~dop.def.sesFileChk,... % if not running session summary
                    dop.def.sesFileChk && dop.ses.session==dop.ses.nSes+1) % if session summary, only if last session
                fprintf('\t%s\n','> > Adding new line to labels...');
                fprintf(dop.tmp.fid,'\n'); % new line at the end
            end
            fclose(dop.tmp.fid);
        end
        %% > data
        %% >> session concatenation
        % can't write data during session summary until labels are
        % completed (after all summary)
        if dop.def.sesFileChk && dop.ses.ind==1
            if ~isfield(dop.ses,'savData')
                dop.ses.savData=dop.sav.data;
            else
                for i=1:length(dop.sav.data) % write each data value
                    dop.ses.savData{end+1}=dop.sav.data{i};
                end
            end
            if dop.ses.session==dop.ses.nSes+1 % all summary
                % put the data in the regular place
                dop.sav.data=dop.ses.savData;
            end
        end
        if or(~dop.def.sesFileChk,... % if not running session summary, save data
                dop.def.sesFileChk && ...
                or(dop.ses.ind==1 && dop.ses.session==dop.ses.nSes+1,... % if first person, have to write the labels before the data
                dop.ses.ind~=1)) % otherwise, write as we go
            dop.tmp.fid=fopen(dop.sav.fileNameLoc,'a+'); % open the file to append
            for i=1:length(dop.sav.data) % write each label
                if isnumeric(dop.sav.data{i}) || islogical(dop.sav.data{i})
                    fprintf(dop.tmp.fid,'%+3.2f\t',dop.sav.data{i});
                else
                    fprintf(dop.tmp.fid,'%s\t',dop.sav.data{i});
                end
            end
            if or(~dop.def.sesFileChk,... % if not running session summary
                    dop.def.sesFileChk && dop.ses.session==dop.ses.nSes+1) % if session summary, only if last session
                fprintf('\t%s\n','> > Adding new line to data...');
                fprintf(dop.tmp.fid,'\n'); % new line at the end
            end
            fclose(dop.tmp.fid);
        end
        %% clear tmp
        if isfield(dop,'tmp')
            dop=rmfield(dop,'tmp');
        end
        
        fprintf('\t%s\n','Done');
    else
        fprintf('\t%s\n\t%s\n','Data requested not to be saved',...
            '(see dopTaskDef or gui settings)');
    end
catch err
    %% catch dopOSCCI error
    %
    save(dopOSCCIdebug(mfilename));rethrow(err);
    %     dopOSCCIcrash(err,dop,mfilename);
end

% clear gr inputs
end
% %% ------------------------------------
% %% embedded:
%
