clear all
close all

%fid = fopen('CPF003.001r1.msg');
fid = fopen('CPF003.001.msg');

rowIndex = 1;
getHeaderLine = true;
while ~feof(fid)
    inLine = fgetl(fid)
    if(getHeaderLine)
        if strncmp(inLine, "Comment", 7)
          dataLines{rowIndex} = vertcat(strsplit(inLine, '\t'))
          rowIndex = rowIndex + 1;
          getHeaderLine = false;
        end
    end
    
    if(strncmp(inLine, "AirCal", 6) || ...
        strncmp(inLine, "AnchorPt", 8) || ... 
        strncmp(inLine, "AscendPt", 8))
          dataLines{rowIndex} = vertcat(strsplit(inLine, '\t'))
          rowIndex = rowIndex + 1;
    end    
end
fclose(fid);

dataLines = dataLines';

dataLines = vertcat(dataLines{:});
varTypes = {'string', 'string', 'double', 'double', 'double', 'double', 'double', 'double', ...
    'double', 'double', 'double', 'double', 'double', 'double', 'double', 'double', 'double'};

% I'd like to do this:
%dataTable = array2table(dataLines(2:end,:), 'VariableNames', dataLines(1,:), 'VariableTypes', varTypes);
% but array2table doesn't recognize 'VariableTypes'

tempDataTable = array2table(dataLines(2:end,:), 'VariableNames', dataLines(1,:));
writetable(tempDataTable, 'dataTable.txt')
dataTable = readtable('dataTable.txt', 'Format', '%s%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f')

dataTable.dt = datetime(dataTable.DateTime, 'InputFormat', 'yyyy-MMM-dd''T''HH:mm:ss''Z')

% If I can define the variable types in the table I won't have to use
% str2double to do things like this:
plot(dataTable.dt, str2double(dataTable.pres_dBar))


    
    