%% 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 18:00:08

%% Initialize variables.
filename = 'Z:\Data\ParagonTests\2014Nov26\2014-11-26T17-44-7.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,7,10,11,12,13,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]
    % 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

dateFormats = {'dd-MMM-yyyy', 'HH:mm:ss.SSS'};
dateFormatIndex = 1;
blankDates = cell(1,size(raw,2));
anyBlankDates = false(size(raw,1),1);
invalidDates = cell(1,size(raw,2));
anyInvalidDates = false(size(raw,1),1);
for col=[1,2]% Convert the contents of columns with dates to MATLAB datetimes using date format string.
    try
        dates{col} = datetime(dataArray{col}, 'Format', dateFormats{col==[1,2]}, 'InputFormat', dateFormats{col==[1,2]}); %#ok<SAGROW>
    catch
        try
            % Handle dates surrounded by quotes
            dataArray{col} = cellfun(@(x) x(2:end-1), dataArray{col}, 'UniformOutput', false);
            dates{col} = datetime(dataArray{col}, 'Format', dateFormats{col==[1,2]}, 'InputFormat', dateFormats{col==[1,2]}); %%#ok<SAGROW>
        catch
            dates{col} = repmat(datetime([NaN NaN NaN]), size(dataArray{col})); %#ok<SAGROW>
        end
    end
    
    dateFormatIndex = dateFormatIndex + 1;
    blankDates{col} = cellfun(@isempty, dataArray{col});
    anyBlankDates = blankDates{col} | anyBlankDates;
    invalidDates{col} = isnan(dates{col}.Hour) - blankDates{col};
    anyInvalidDates = invalidDates{col} | anyInvalidDates;
end
dates = dates(:,[1,2]);
blankDates = blankDates(:,[1,2]);
invalidDates = invalidDates(:,[1,2]);

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


%% 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 = dates{:, 1};
Time = dates{:, 2};
State = rawCellColumns(:, 1);
Comment = rawCellColumns(:, 2);
Pressure = cell2mat(rawNumericColumns(:, 1));
BelPos = cell2mat(rawNumericColumns(:, 2));
BattVolt = cell2mat(rawNumericColumns(:, 3));
GPS = rawCellColumns(:, 3);
IridModem = rawCellColumns(:, 4);
LPS331Press = cell2mat(rawNumericColumns(:, 4));
LPS311Temp = cell2mat(rawNumericColumns(:, 5));
SHT21Humid = cell2mat(rawNumericColumns(:, 6));
SHT21Temp = cell2mat(rawNumericColumns(:, 7));
CTDTemp = rawCellColumns(:, 5);
CTDSal = rawCellColumns(:, 6);
Topt = rawCellColumns(:, 7);
Tphase = rawCellColumns(:, 8);
Rphase = rawCellColumns(:, 9);
NO3 = rawCellColumns(:, 10);
pH = rawCellColumns(:, 11);
FSig = rawCellColumns(:, 12);
BBSig = rawCellColumns(:, 13);
TSig = rawCellColumns(:, 14);
LPFPVel = cell2mat(rawNumericColumns(:, 8));
DiffPVel = cell2mat(rawNumericColumns(:, 9));
LPFPAcc = cell2mat(rawNumericColumns(:, 10));
PressAvg = cell2mat(rawNumericColumns(:, 11));
PressVar = cell2mat(rawNumericColumns(:, 12));
PIDSP = cell2mat(rawNumericColumns(:, 13));
PIDPV = cell2mat(rawNumericColumns(:, 14));
PIDY1 = cell2mat(rawNumericColumns(:, 15));
PIDY2 = cell2mat(rawNumericColumns(:, 16));
PIDI = cell2mat(rawNumericColumns(:, 17));
PIDv = cell2mat(rawNumericColumns(:, 18));
PIDu = cell2mat(rawNumericColumns(:, 19));
PIDuCPS = cell2mat(rawNumericColumns(:, 20));
BattAmps = cell2mat(rawNumericColumns(:, 21));
x12VAmps = cell2mat(rawNumericColumns(:, 22));
x3_3VAmps = cell2mat(rawNumericColumns(:, 23));

% For code requiring serial dates (datenum) instead of datetime, uncomment
% the following line(s) below to return the imported dates as datenum(s).

% Date=datenum(Date);
% Time=datenum(Time);


%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me dateFormats dateFormatIndex dates blankDates anyBlankDates invalidDates anyInvalidDates rawNumericColumns rawCellColumns R;