load('G:\Fixations\data\EventFixDynamic.mat')

for e=1:2
    exp=fieldnames(EventFix);
    exp=exp(e,1);
    exp=exp{1,1};
    for s=1:length(fieldnames(EventFix.(exp)))
        subj=fieldnames(EventFix.(exp));
        subj=subj(s,1);
        subj=subj{1,1};
            
        source=sprintf('C:\\Users\\Veronika\\Desktop\\EEG\\%s\\raw_data\\synchro_%s_%s.asc.eeg.mat', exp, subj, exp);
        load(source);
        
        i=find(EegAcq.Events.Triggers.value==30);
        onset=EegAcq.Events.Triggers.time(1,i); % timestamp for onset
        j=find(EegAcq.Events.Triggers.value==60);
        offset=EegAcq.Events.Triggers.time(1,j); % timestamp for offset
        duration=offset-onset; % corresponding duration of epoch
        EventStim=cell(500,3);
        
        ind=1;
        for ImInd=1:length(EventFix.(exp).(subj))
            if ischar(EventFix.(exp).(subj)(1, ImInd).Nature)
                Emotion=EventFix.(exp).(subj)(1, ImInd).Nature;
            else
                Emotion=EventFix.(exp).(subj)(1, ImInd).Nature{1,1};
            end
            switch Emotion 
                case 'distracteur'
                    mark1=60;
                    name1='Distracteur';
                case 'N' %neutral
                    mark1=0;
                    name1='Neutral';
                case 'F' %fear
                    mark1=10;
                    name1='Fear';
                case 'D' %disgust
                    mark1=20;
                    name1='Disgust';
                case 'S' %surprise
                    mark1=30;
                    name1='Surprise';
                case 'G' %happiness
                    mark1=40;
                    name1='Happy';
            end
            EventStim(ind,:)={mark1,name1,onset(ImInd)};
            EventFix.(exp).(subj)(1, ImInd).Onset(1,1) = onset(ImInd);
            EventFix.(exp).(subj)(1, ImInd).Onset(2,1) = mark1+2000;
            ind=ind+1;
            mark2=50; %for all emotions(everyone gets it)
            name2='';
            if isempty(EventFix.(exp).(subj)(1, ImInd).Roi)
            else
                for n=1:length(EventFix.(exp).(subj)(1, ImInd).Roi)
                    dur=length(EventFix.(exp).(subj)(1, ImInd).Roi{1, n});
                    if dur>=1800 && dur<=50 % condition for the length of the fixation
                       % no marker
                    else            
                        roi=find(EventFix.(exp).(subj)(1, ImInd).Roi{1, n},1);
                        if (onset(ImInd)+2200)>(EventFix.(exp).(subj)(1, ImInd).Tab(1,n))
                            Ap=100;
                            nameAp='Preapex';
                        else
                            Ap=200;
                            nameAp='Afterapex';
                        end
                        Apany=300;
                        nameApany='fix';
                        switch roi
                            case {1 , 2} % eyes
                                mark3=1;
                                name3='Eyes';
                            case {3 , 7} % brows
                                mark3=2;
                                name3='Brows';
                            case 4 % mouth
                                mark3=3;
                                name3='Mouth';
                            case 5 % chin
                                mark3=4;
                                name3='Chin';
                            case 6 % nose
                                mark3=5;
                                name3='Nose';
                            case 8 % corr
                                mark3=6;
                                name3='Corr';
                            case 9 % front
                                mark3=7;
                                name3='Front';
                            case 10 
                                mark3=[];
                                name3='';
                        end
                        if isempty(mark3)
                        else
                            mark4=0;
                            name4='';
                            time=EventFix.(exp).(subj)(1, ImInd).Tab(1,n);
                            %% anywhere
                            EventFix.(exp).(subj)(1, ImInd).Tab(5,n)=Apany+mark2+2000; %fix anywhere
                            EventStim(ind,:)={Apany+mark2,strcat(nameApany,name2),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(6,n)=Apany+mark1+2000; %fix in emotion
                            EventStim(ind+1,:)={Apany+mark1,strcat(nameApany, name1),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(7,n)=Apany+mark2+mark3+2000; %fix localization
                            EventStim(ind+2,:)={Apany+mark3+mark2,strcat(nameApany,name3),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(8,n)=Apany+mark1+mark3+2000; %fix emotion localization
                            EventStim(ind+3,:)={Apany+mark1+mark3,strcat(nameApany, name1, name3),time};
                            ind=ind+4;
                            %% preapex/afterapex
                            EventFix.(exp).(subj)(1, ImInd).Tab(9,n)=Ap+mark2+2000; %fix anywhere
                            EventStim(ind,:)={Ap+mark2,strcat(nameAp,name2),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(10,n)=Ap+mark1+2000; %fix in emotion
                            EventStim(ind+1,:)={Ap+mark1,strcat(nameAp, name1),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(11,n)=Ap+mark3+mark2+2000; %fix localization
                            EventStim(ind+2,:)={Ap+mark3+mark2,strcat(nameAp,name3),time};
                            EventFix.(exp).(subj)(1, ImInd).Tab(12,n)=Ap+mark1+mark3+2000; %fix emotion localization
                            EventStim(ind+3,:)={Ap+mark1+mark3,strcat(nameAp, name1, name3),time};
                            ind=ind+4;
                        end
                    end
                end
            end          
        end
        txtfilename=sprintf('synchro_%s_%s_fix.txt', exp, subj); 
        saveCellToFile(txtfilename, EventStim)
    end
end

%% Save static data as EventFixStatic.mat
save('G:\Fixations\data\EventFixDynamic.mat', 'EventFix')