function T215445 = importfile1(filename, startRow, endRow)
%IMPORTFILE1 Import numeric data from a text file as a matrix.
%   T215445 = IMPORTFILE1(FILENAME) Reads data from text file FILENAME for
%   the default selection.
%
%   T215445 = IMPORTFILE1(FILENAME, STARTROW, ENDROW) Reads data from rows
%   STARTROW through ENDROW of text file FILENAME.
%
% Example:
%   T215445 = importfile1('2016-11-22T21-54-45.eng', 2, 1138);
%
%    See also TEXTSCAN.

% Auto-generated by MATLAB on 2016/11/23 09:25:44

%% Initialize variables.
delimiter = '\t';
if nargin<=2
    startRow = 2;
    endRow = inf;
end

%% 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, endRow(1)-startRow(1)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false);
    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end

%% 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=[6,7,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,36]
    % 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(numbers, 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

% Convert the contents of columns with dates to MATLAB datetimes using date
% format string.
try
    dates{2} = datetime(dataArray{2}, 'Format', 'dd-MMM-yyyy', 'InputFormat', 'dd-MMM-yyyy');
catch
    try
        % Handle dates surrounded by quotes
        dataArray{2} = cellfun(@(x) x(2:end-1), dataArray{2}, 'UniformOutput', false);
        dates{2} = datetime(dataArray{2}, 'Format', 'dd-MMM-yyyy', 'InputFormat', 'dd-MMM-yyyy');
    catch
        dates{2} = repmat(datetime([NaN NaN NaN]), size(dataArray{2}));
    end
end

anyBlankDates = cellfun(@isempty, dataArray{2});
anyInvalidDates = isnan(dates{2}.Hour) - anyBlankDates;
dates = dates(:,2);

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

%% Create output variable
T215445 = table;
T215445.VarName1 = rawCellColumns(:, 1);
T215445.Date = dates{:, 1};
T215445.Time = rawCellColumns(:, 2);
T215445.State = rawCellColumns(:, 3);
T215445.Comment = rawCellColumns(:, 4);
T215445.Pressure = cell2mat(rawNumericColumns(:, 1));
T215445.BelPos = cell2mat(rawNumericColumns(:, 2));
T215445.BattVolt = cell2mat(rawNumericColumns(:, 3));
T215445.BattAmps = cell2mat(rawNumericColumns(:, 4));
T215445.CTDTemp = rawCellColumns(:, 5);
T215445.CTDSal = cell2mat(rawNumericColumns(:, 5));
T215445.RawPress = cell2mat(rawNumericColumns(:, 6));
T215445.LPFPress = cell2mat(rawNumericColumns(:, 7));
T215445.LPFVel = cell2mat(rawNumericColumns(:, 8));
T215445.DiffVel = cell2mat(rawNumericColumns(:, 9));
T215445.Diff2Vel = cell2mat(rawNumericColumns(:, 10));
T215445.SlopeofLPFPress = cell2mat(rawNumericColumns(:, 11));
T215445.CTDSamplePeriod = cell2mat(rawNumericColumns(:, 12));
T215445.LPS331Press = cell2mat(rawNumericColumns(:, 13));
T215445.LPS311Temp = cell2mat(rawNumericColumns(:, 14));
T215445.SHT21Humid = cell2mat(rawNumericColumns(:, 15));
T215445.SHT21Temp = cell2mat(rawNumericColumns(:, 16));
T215445.PIDSP = cell2mat(rawNumericColumns(:, 17));
T215445.PIDPV = cell2mat(rawNumericColumns(:, 18));
T215445.PIDY1 = cell2mat(rawNumericColumns(:, 19));
T215445.PIDY2 = cell2mat(rawNumericColumns(:, 20));
T215445.PIDI = cell2mat(rawNumericColumns(:, 21));
T215445.PIDv = cell2mat(rawNumericColumns(:, 22));
T215445.PIDu = cell2mat(rawNumericColumns(:, 23));
T215445.PIDuCPS = cell2mat(rawNumericColumns(:, 24));
T215445.PIDh = cell2mat(rawNumericColumns(:, 25));
T215445.FLBB = rawCellColumns(:, 6);
T215445.Topt = rawCellColumns(:, 7);
T215445.Tphase = rawCellColumns(:, 8);
T215445.Rphase = rawCellColumns(:, 9);
T215445.NO3 = cell2mat(rawNumericColumns(:, 26));
T215445.pH = rawCellColumns(:, 10);
T215445.GPS = rawCellColumns(:, 11);
T215445.IridModem = rawCellColumns(:, 12);

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

% T215445.Date=datenum(T215445.Date);

