clc;clear all;
%% Additional fields into dynamic data
% Status to emotion recognition, T/F
% Arousal answer rewritten to a scale <-2, 2>
% FixInVid matrixe with all fixation and its length throughout the
% stimulation

%%
%load structure for dynamic
load 'G:\Fixations\Data\EventFix_Dynemo_Dest.mat'

for a=1:length(fieldnames(EventFix))
    efed=fieldnames(EventFix);
    efed=efed(a,1);
    efed=efed{1,1};
    for i=1:length(fieldnames(EventFix.(efed)))
        fn=fieldnames(EventFix.(efed));
        fn=fn(i,1);
        fn=fn{1,1};
        for j=1:1:length(EventFix.(efed).(fn))
            if strcmp('EFED2', efed)
                nature=EventFix.(efed).(fn)(1,j).Nature{1,1};
                answer=EventFix.(efed).(fn)(1,j).EmotionAnswer;
                arousal=EventFix.(efed).(fn)(1,j).ArousalAnswer;
                %% Compares real nature with answer, true=1, false=0
                if strcmp(nature, answer)
                    EventFix.(efed).(fn)(1, j).AnswerStatus =1;
                else
                    EventFix.(efed).(fn)(1, j).AnswerStatus =0;
                end
                %% Rewrites string arousal with numbers - scale <-2,2> 
                if arousal=='--'
                    EventFix.(efed).(fn)(1, j).ArousalAnswer = -2;
                elseif arousal=='-'
                    EventFix.(efed).(fn)(1, j).ArousalAnswer = -1;
                elseif arousal==0
                    EventFix.(efed).(fn)(1, j).ArousalAnswer = 0;
                elseif arousal=='+'
                    EventFix.(efed).(fn)(1, j).ArousalAnswer = 1;
                elseif arousal=='++'
                    EventFix.(efed).(fn)(1, j).ArousalAnswer = 2;
                end
            end            
            NbFix=EventFix.(efed).(fn)(1,j).NbFix;
            mat=zeros(13,5000);
            if isempty(EventFix.(efed).(fn)(1,j).Roi)
                    disp('ROI has not been evaluated for this video and subject')
                    mat=[];
            else
                 for k=1:1:NbFix
                    frame1=EventFix.(efed).(fn)(1,j).Frame(1,k);
                    frame2=frame1+1;
                    frame3=EventFix.(efed).(fn)(1,j).Frame(3,k);
                    pre=EventFix.(efed).(fn)(1,j).Frame(2,k);

                    past=EventFix.(efed).(fn)(1,j).Frame(4,k);
                    suma=pre+ past + ((frame3-frame2)*40); % time of the fixation
                    start=(frame1*40)-pre;
                    total=sum(EventFix.(efed).(fn)(1,j).Roi{1,k},2);
                    if total(1:9,1)~=0
                       [a,b]=max(total(1:9,1));
                    else
                       [a,b]=max(total);
                    end
                    for l=start:1:start+suma-1
                        mat(b,l)=1;
                    end
                 end
            end
           EventFix.(efed).(fn)(1, j).FixInVid = mat;
        end
    end
end
%% Save dynamic data as EventFixDynamic.mat
save('G:\Fixations\Data\EventFixDynamic.mat', 'EventFix')


