clf
%clear all

G = 1.5;

dirname = uigetdir;

cd(dirname);

textFiles = dir('*.txt');
numfiles = length(textFiles);


Data = [];
DataAll = [];
x = [1:1:991];

for i=1:numfiles
    filename = textFiles(i).name;
    %filename = uigetfile('*.txt')
    %uiopen(filename);
    %filename = '30mil.txt';
    fileID = fopen(filename);

    C = textscan(fileID,'%s %f32 %f32 %f32 %f32 %f32', 'Delimiter', ',');

    fclose('all');

    SerialNumber = C{1}{1};

    Distance = C{1,2};
    Distance = Distance(1,1);
    DistanceStr = num2str(Distance);

    Force = C{1,3};
    Force = Force(1,1);
    ForceStr = num2str(Force);

    ShortPulse = C{1,4};
    ShortPulse = ShortPulse(1:1000,1);

    LongPulse = C{1,5};
    LongPulse = LongPulse(1:1000,1);

    ShortDev = std(LongPulse);
    LongDev = std(LongPulse);

    ShortPulseMean = mean(ShortPulse);
    LongPulseMean = mean(LongPulse);

    ShortPercent = ShortDev / ShortPulse * 100;
    LongPercent = LongDev / LongPulse * 100;

    Diff = LongPulseMean - G*ShortPulseMean;
    
    Data = [Distance Force ShortPulseMean LongPulseMean Diff ShortDev LongDev];
    DataAll = [DataAll; Data];
end

text = strcat(SerialNumber, {', '}, 'Distance vs Pulse Difference', {', G = 1.5'});

Distance = DataAll(1:i,1);
ShortValues = DataAll(1:i,3);
LongValues = DataAll(1:i,4);
Difference = LongValues - G*ShortValues;

figure(1);
hold on
title(text);
plot(Distance, Difference, '+b', 'LineWidth', 1.5);
%axis([0 50 -525 150]);
xlim([0 50]);
xlabel('Plate Distance (mil)');
ylabel('Output Values (mV)');
hold off


text = strcat(SerialNumber, {', '}, 'Distance vs Two Pulse Times', {', G = 1.5'});

figure(2)
hold on
title(text);
plot(Distance, ShortValues, '*r', 'LineWidth', 1.5);
plot(Distance, LongValues, '+g', 'LineWidth', 1.5);
%axis([0 50 2200 5200]);
xlim([0 50]);
legend('Short Pulse', 'Long Pulse', 'Location', 'Southeast');
hold off

if SerialNumber == 'SN07'
    SN07_Diff = DataAll(1:i,5);
elseif SerialNumber == 'SN11'
    SN11_Diff = DataAll(1:i,5);
elseif SerialNumber == 'SN18'
    SN18_Diff = DataAll(1:i,5);
end
