function mytable = importfile1(filename, startRow, endRow)
%% Import data from text file.
% Script for importing data from the following text file:
%
%    \\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution9_25Apr2018_C113.csv
%
% 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 2018/04/26 17:04:31

%% Initialize variables.
%filename = '\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution9_25Apr2018_C113.csv';
delimiter = ',';
startRow = 2;

%% 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%[^\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, 'HeaderLines' ,startRow-1, '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=[12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
    % 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


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


%% 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
mytable = table;
mytable.Plate = rawCellColumns(:, 1);
mytable.Experiment = rawCellColumns(:, 2);
mytable.Group = rawCellColumns(:, 3);
mytable.Filename = rawCellColumns(:, 4);
mytable.Sample = rawCellColumns(:, 5);
mytable.Workspace = rawCellColumns(:, 6);
mytable.CompensationSource = rawCellColumns(:, 7);
mytable.PlotTitle = rawCellColumns(:, 8);
mytable.Gate = rawCellColumns(:, 9);
mytable.XParameter = rawCellColumns(:, 10);
mytable.YParameter = rawCellColumns(:, 11);
mytable.Count = cell2mat(rawNumericColumns(:, 1));
mytable.Volume = cell2mat(rawNumericColumns(:, 2));
mytable.Concentration = cell2mat(rawNumericColumns(:, 3));
mytable.Total = cell2mat(rawNumericColumns(:, 4));
mytable.Gated = cell2mat(rawNumericColumns(:, 5));
mytable.XMean = cell2mat(rawNumericColumns(:, 6));
mytable.YMean = cell2mat(rawNumericColumns(:, 7));
mytable.XMedian = cell2mat(rawNumericColumns(:, 8));
mytable.YMedian = cell2mat(rawNumericColumns(:, 9));
mytable.XPeak = cell2mat(rawNumericColumns(:, 10));
mytable.YPeak = cell2mat(rawNumericColumns(:, 11));
mytable.XSD = cell2mat(rawNumericColumns(:, 12));
mytable.YSD = cell2mat(rawNumericColumns(:, 13));
mytable.XCV = cell2mat(rawNumericColumns(:, 14));
mytable.YCV = cell2mat(rawNumericColumns(:, 15));
mytable.XrSD = cell2mat(rawNumericColumns(:, 16));
mytable.YrSD = cell2mat(rawNumericColumns(:, 17));
mytable.XrCV = cell2mat(rawNumericColumns(:, 18));
mytable.YrCV = cell2mat(rawNumericColumns(:, 19));

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