close all
clear all

global P;
global count;

path = '.\';





grav = 9.81;  %m/s^2
epoch_serialdate = datenum(1970,1,1,0,0,0);

%Packet Structure
DataSize = 108;
S.EpochUsecs = uint32(NaN);
S.EpochSecs = uint32(NaN);
S.rollAngle = int16(NaN);
S.pitchAngle = int16(NaN);
S.yawAngleTrue = int16(NaN);
S.xRateCorrected = int16(NaN);
S.yRateCorrected = int16(NaN);
S.zRateCorrected = int16(NaN);
S.xAccel = int16(NaN);
S.yAccel = int16(NaN);
S.zAccel = int16(NaN);
S.northVelocity = int16(NaN);
S.eastVelocity = int16(NaN);
S.downVelocity = int16(NaN);
S.longitudeGPS = int32(NaN);
S.latitudeGPS = int32(NaN);
S.altitudeGPS = int16(NaN);
S.xRateTemperature = int16(NaN);
S.timeITOW = uint32(NaN);
S.statusBits = int16(NaN);

S.padBytes = uint16(NaN);

S.LoadDumpVoltage = double(NaN);
S.BatteryVoltage = double(NaN);
S.BatteryCurrent = double(NaN);

S.UpperSpringTemperature = int32(NaN);
S.LowerSpringTemperature = int32(NaN);
S.SeawaterTemperature = int32(NaN);
S.IsothermalTemperature = int32(NaN);
S.UpperSpringPressure = int32(NaN);
S.LowerSpringPressure = int32(NaN);
S.LoadCellForce = int32(NaN);
S.PistonDistance = int32(NaN);



%filenames = {'D000002_110125_110458.mat','D000003_110125_120458.mat','D000004_110125_130458.mat','D000005_110125_140458.mat'};
filenames = {'FullDataSet.mat'};

for i = 1:length(filenames)
    filenames{i}
  load([path filenames{i}]);
  names = fieldnames(Eng);
    for j = 1:length(names)
      if (~exist(['cat_' names{j}]))
        ['cat_' names{j} ' = NaN;']
        eval(['cat_' names{j} ' = NaN;']);  
      end
    end
    
    for j = 1:length(names)
        ['cat_' names{j} ' = [ cat_' names{j} ' ' names{j} '];']
        eval(['cat_' names{j} ' = [ cat_' names{j} ' ' names{j} '];']);
    end
end
  

idx = find((cat_serialdate > datenum('25-Jan-2011 19:30:00')) & (cat_serialdate < datenum('25-Jan-2011 22:15:00')));

 for j = 1:length(names)
        str = [names{j} ' =  cat_' names{j} '; clear cat_' names{j}]
        eval(str);    
 end

 for j = 1:length(names)
        str = [names{j} ' = ' names{j} '(idx);']
        eval(str);    
 end
 
% clear i j idx idx1;

 
save([path 'Concatenated'])


