clc;clear all;
%% Additional fields into static data structure
% 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 static
load 'G:\Fixations\Data\EventFix_Dynemo_Dest2.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('EFE2', efed)
                %% Add AnswerStatus field
                nature=EventFix.(efed).(fn)(1,j).Nature{1,1};
                answer=EventFix.(efed).(fn)(1,j).EmotionAnswer;
                arousal=EventFix.(efed).(fn)(1,j).ArousalAnswer;
                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            
            %% Create matrix for fixations FixInVid
            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
                start=2;
                 for k=1:1:NbFix
                    suma=EventFix.(efed).(fn)(1,j).Tab(4,k);                    
                    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
                    start=start+suma+1;
                 end
            end
           EventFix.(efed).(fn)(1, j).FixInVid = mat;
        end

    end
end

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

