clear
close all
clc



controlFrequency = 1000;
initialWait = 10;
twoChannels = true;

matfiles = dir('*.mat');

strain = {};
strain_labels = {};

strainS = {};
strainS_labels = {};
voltages = num2cell(3000 + (0:500:7000)');


freq = {};
freq_labels = {};
frequencies = num2cell((2:2:20)');

for i = 1:length(matfiles)
    fname = matfiles(i).name;
    nsplit = split(fname, '_');
    id = nsplit{2};
    disp(fname)
    
    load(fname);
    
    failed = checkForFailure(data, controlFrequency, frequency, voltage, cycles, twoChannels);
    
    if failed
        fprintf(2, '%s failed\n', id)
        continue;
    end
    
    if time(end) < 30 % not a stability test
        if frequency <= 2 % not a frequency test --> record strain
            [ smean, smin, smax, sstdev ] = getActuationStrain(data, frequency, cycles, controlFrequency, initialWait);

            % warn if stdev is too large
            if sstdev/smean > 0.1
                fprintf(2,'large stdev! mean: %i, stdev: %i\n', smean, sstdev)
            end


            if id(end) == 's'
                % find column
                if isempty(strainS_labels)
                    c = 1;
                    strainS_labels{end+1} = id;
                elseif strcmp(strainS_labels{end}, id)
                    c = length(strainS_labels);
                else
                    c = length(strainS_labels) + 1;
                    strainS_labels{end+1} = id;
                end

                % find row
                r = (voltage-3000) / 500 + 1;

                % check if a value already exists, just in case
                if all(size(strainS) >= [r, c]) && ~isempty(strainS{r, c})
                    fprintf(2, 'overwriting previous value\n')
                end

                strainS{r, c} = smean;

            else
                % find column
                if isempty(strain_labels)
                    c = 1;
                    strain_labels{end+1} = id;
                elseif strcmp(strain_labels{end}, id)
                    c = length(strain_labels);
                else
                    c = length(strain_labels) + 1;
                    strain_labels{end+1} = id;
                end

                % find row
                r = (voltage-3000) / 500 + 1;

                % check if a value already exists, just in case
                if all(size(strain) >= [r, c]) && ~isempty(strain{r, c})
                    fprintf(2, 'overwriting previous value\n')
                end

                strain{r, c} = smean;

            end
        end
%         if voltage == 6000 % get frequency response
%             [ ampl ] = getAmplitude(data);
% 
%             % find column
%             if isempty(freq_labels)
%                 c = 1;
%                 freq_labels{end+1} = id;
%             elseif strcmp(freq_labels{end}, id)
%                 c = length(freq_labels);
%             else
%                 c = length(freq_labels) + 1;
%                 freq_labels{end+1} = id;
%             end
% 
%             % find row
%             r = frequency/2;
% 
%             % check if a value already exists, just in case
%             if all(size(freq) >= [r, c]) && ~isempty(freq{r, c})
%                 fprintf(2, 'overwriting previous value\n')
%             end
% 
%             freq{r, c} = ampl;
% 
%         end
        
    else % cycle stability test
        
    end
    
end

%% write data

xlsdata = [[{[]}, strain_labels]; [voltages(1:size(strain, 1)), strain]];
xlswrite('hv_results.xls', xlsdata, 'no-stretch')
xlsdataS = [[{[]}, strainS_labels]; [voltages(1:size(strainS, 1)), strainS]];
xlswrite('hv_results.xls', xlsdataS, 'stretch')
% xlsdataS = [[{[]}, freq_labels]; [frequencies(1:size(freq, 1)), freq]];
% xlswrite('hv_results.xls', xlsdataS, 'freq-response')

