%% XLSexportDuration is a function that creates a .xls file of durations of fixations in ROI per emotion (all subjects)
%% Input data
% path: path to fixation data
% efed: experiment (EFE1/EFE2; EFED1/EFED2)
% way: path to save the file
% limitdwn: minimum duration of fixation in ms
% limitup: maximum duration of fixation in ms
%% Output data
% .xls file where one sheet is per emotion and roi

function XLSexportDuration(path,efed,way,limitdwn, limitup)
%% emotions
emotion={'D','S','N','F','G'};
%% rois
roid=struct('eyes',1,'brows',2,'mouth',3,'chin',4,'nose',5,'corr',6,'front',7);
%% load data
load(path);

%% searching for the particular videos for chosen emotion, roi, for all subjects
s=struct;
for a=1:length(emotion) % for every emotion
    emo=emotion(1,a); % a emotion
    for b=1:length(fieldnames(roid)) % for every region of interest
        fn=fieldnames(roid);
        fn=fn{b,1};
        roi=roid.(fn); % b region of interest 
        zk=[]; % initiate
        for i=1:length(fieldnames(EventFix.(efed))) % for every subject
            subj=fieldnames(EventFix.(efed));
            subj=subj(i,1);
            subj=subj{1,1}; % i subject
            p=0; 
            for j=1:length(EventFix.(efed).(subj)) % for every video
                nature=EventFix.(efed).(subj)(1,j).Nature; 
                if strcmp (nature, emo)
                   if isempty(EventFix.(efed).(subj)(1,j).Roi) %if doesnt contain ROI, then move on
                      continue
                   else 
                      B.NumObjects=[]; % initiate empty vector for each video
                      nmat=zeros(7,5000); % initiate empty matrix to put in the fixations
                      mat=EventFix.(efed).(subj)(1,j).FixInVid; % get matrix of fixations from structure
                      %% adjust to proper rois
                      nmat(1,:)=mat(1,:)+mat(2,:); 
                      nmat(2,:)=mat(3,:)+mat(7,:);
                      nmat(3:5,:)=mat(4:6,:);
                      nmat(6:7,:)=mat(8:9,:);
                      %% find the fixation with duration in the chosen limit for roi
                      B=bwconncomp(nmat(roi,:));
                      NumObjects=B.NumObjects;
                      if NumObjects>0
                          for k=1:NumObjects
                              p=p+1;
                              dur=length(B.PixelIdxList{1,k});
                              if dur>=limitup || dur<=limitdwn
                                  zk(i,p)=0;
                              else
                                  zk(i,p)=dur;
                              end
                          end
                      end
                   end
                else
                    p=p+1;
                    zk(i,p)=0;
                    continue
                end
            end
        end
        zk(zk==0)=NaN; % get rid of fake zero durations
        zk=sort(zk,2); % sort durations
        anotherfield=sprintf('%s%d',emo{1,1}, roi); % create name for new List
        s(:).(anotherfield)=zk; % saves durations in the s structure
    end
end

%% export .xls file
filename=sprintf('%s_durations_%d_%dms.xls', efed, limitdwn, limitup); % generate .xls name
names=fieldnames(s); 
subjects=fieldnames(EventFix.(efed));
for i=1:length(names) % i number of sheets 
    A=s.(names{i}); 
    xlswrite([way '\' filename], [subjects num2cell(A)], names{i}); % Duration for emotion in roi per one sheet
end
