function [totalStrain,imgNums] = readMatFiles(dataFolder)
% Get total extensile strain from VIC DIC .mat file outputs
%
% By default, strain is computed along the centerline of the VIC ROI,
% from the third grid point on the left and going to the third-from-right
% grid point of the DIC analysis. Strain is averaged along the line, so
% only one value (total strain along the line) is output - the intent is
% that this coresponds to the gauge strain that would be measured with an
% extensometer.
%
% *** INPUT ***
%  dataFolder - a folder containing only the .mat results files from a VIC
%               analysis
% *** OUTPUT ***
%  totalStrain - a vector of strain values versus frame (i.e. time-ish)
%
% Orion Kafka
% 05-15-2017
% 02-05-2018 Modified to average across the 5 center-most lines
% 02-15-2018 Modified to find a region where there are no missing 
%            (uncorrelated) poitns 
% Tested on MATLAB 2017a for Windows; should be portable.

% Get strain from VIC DIC datafiles
vicFolder = dir(fullfile(dataFolder));

% Set up variables
totalStrain = zeros(size(vicFolder,1)-2,1);

% Find a position where there are strain values for the whole gauge length
done=0;
ii = size(vicFolder,1)-2;
while done~=1
   vicData = load(fullfile(vicFolder(ii+2).folder,vicFolder(ii+2).name));

    pos = floor(size(vicData.exx,1)/2.5);
    % Go from third from left to third from right, and collect the strain
    % along the line defined above and check that there are no non-zero entries
    % in the area
    while sum(sum(vicData.exx(pos-2:pos+2,3:end-3)==0)) >= 1
        pos = pos +1;
        if pos > size(vicFolder,1)-3
            done = 0; % go to the previous image and try again
            break
        else
            done = 1;
            break
        end
    end
    disp(['Using mat file: ',num2str(ii)]);
    ii = ii - 1;
end

for ii = 1:size(vicFolder,1)-2
    vicFile = fullfile(vicFolder(ii+2).folder,vicFolder(ii+2).name);
    vicData(ii,:,:) = load(vicFile);
    %pos = floor(size(vicData(ii).exx,1)/4); % Use the centerline
    % Go from third from left to third from right, and collect the strain
    % along the line defined above
    
    for jj = 3:length(vicData(ii).exx(pos,3:end-3))-3
        totalStrain(ii,1) = ...
            vicData(ii).exx(pos-2,jj)/length(vicData(ii).exx(pos-2,3:end-3)) + totalStrain(ii);
        totalStrain(ii,2) = ...
            vicData(ii).exx(pos-1,jj)/length(vicData(ii).exx(pos-1,3:end-3)) + totalStrain(ii);
        totalStrain(ii,3) = ...
            vicData(ii).exx(pos,jj)/length(vicData(ii).exx(pos,3:end-3)) + totalStrain(ii);
        totalStrain(ii,4) = ...
            vicData(ii).exx(pos+1,jj)/length(vicData(ii).exx(pos+1,3:end-3)) + totalStrain(ii);
        totalStrain(ii,5) = ...
            vicData(ii).exx(pos+2,jj)/length(vicData(ii).exx(pos+2,3:end-3)) + totalStrain(ii);
    end
    % Collect file names - this will be used for finding timings later
    idx = strfind(vicFolder(ii+2).name,'_'); % find the location of last "_"
    imgNums{ii} = [vicFolder(ii+2).name(4:6),'-',vicFolder(ii+2).name(idx(end)+1:end-4)];
end

totalStrain = mean(totalStrain,2);

% Save the summary file if we haven't before.
if ~exist(fullfile(dataFolder,'..','totalStrains'))
    save(fullfile(dataFolder,'..','totalStrains'),'totalStrain','imgNums');
end