%% Import data from text file.
% Script for importing data from the following text file:
%
%    Z:\Data\ParagonTests\2014Nov26\2014-11-26T18-26-5.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.

% Auto-generated by MATLAB on 2014/12/01 14:48:40

%% Initialize variables.
filename = 'Z:\Data\ParagonTests\2014Nov26\2014-11-26T18-26-5.txt';
delimiter = '\t';

%% Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

%% Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter,  'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
    raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));

for col=[5,6]
    % Converts strings in the input cell array to numbers. Replaced non-numeric
    % strings with NaN.
    rawData = dataArray{col};
    for row=1:size(rawData, 1);
        % Create a regular expression to detect and remove non-numeric prefixes and
        % suffixes.
        regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
        try
            result = regexp(rawData{row}, regexstr, 'names');
            numbers = result.numbers;
            
            % Detected commas in non-thousand locations.
            invalidThousandsSeparator = false;
            if any(numbers==',');
                thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
                if isempty(regexp(thousandsRegExp, ',', 'once'));
                    numbers = NaN;
                    invalidThousandsSeparator = true;
                end
            end
            % Convert numeric strings to numbers.
            if ~invalidThousandsSeparator;
                numbers = textscan(strrep(numbers, ',', ''), '%f');
                numericData(row, col) = numbers{1};
                raw{row, col} = numbers{1};
            end
        catch me
        end
    end
end


%% Split data into numeric and cell columns.
rawNumericColumns = raw(:, [5,6]);
rawCellColumns = raw(:, [1,2,3,4,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]);


%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells
rawNumericColumns(R) = {NaN}; % Replace non-numeric cells

%% Allocate imported array to column variable names
Date = rawCellColumns(:, 1);
Time = rawCellColumns(:, 2);
State = rawCellColumns(:, 3);
Comment = rawCellColumns(:, 4);
Pressure = cell2mat(rawNumericColumns(:, 1));
BelPos = cell2mat(rawNumericColumns(:, 2));
BattVolt = rawCellColumns(:, 5);
GPS = rawCellColumns(:, 6);
IridModem = rawCellColumns(:, 7);
LPS331Press = rawCellColumns(:, 8);
LPS311Temp = rawCellColumns(:, 9);
SHT21Humid = rawCellColumns(:, 10);
SHT21Temp = rawCellColumns(:, 11);
CTDTemp = rawCellColumns(:, 12);
CTDSal = rawCellColumns(:, 13);
Topt = rawCellColumns(:, 14);
Tphase = rawCellColumns(:, 15);
Rphase = rawCellColumns(:, 16);
NO3 = rawCellColumns(:, 17);
pH = rawCellColumns(:, 18);
FSig = rawCellColumns(:, 19);
BBSig = rawCellColumns(:, 20);
TSig = rawCellColumns(:, 21);
LPFPVel = rawCellColumns(:, 22);
DiffPVel = rawCellColumns(:, 23);
LPFPAcc = rawCellColumns(:, 24);
PressAvg = rawCellColumns(:, 25);
PressVar = rawCellColumns(:, 26);
PIDSP = rawCellColumns(:, 27);
PIDPV = rawCellColumns(:, 28);
PIDY1 = rawCellColumns(:, 29);
PIDY2 = rawCellColumns(:, 30);
PIDI = rawCellColumns(:, 31);
PIDv = rawCellColumns(:, 32);
PIDu = rawCellColumns(:, 33);
PIDuCPS = rawCellColumns(:, 34);
BattAmps = rawCellColumns(:, 35);
VarName38 = rawCellColumns(:, 36);
VarName39 = rawCellColumns(:, 37);


%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns R;