function [TimeStamp,portAD,portBDir,portAOut,portBOut,adcVoltageReading,adcCurrentReading,adxlAccelerometerXReading,adxlAccelerometerYReading,adxlAccelerometerZReading,maximTemperatureReading,bmxAccelerometerXReading,bmxAccelerometerYReading,bmxAccelerometerZReading,bmxAccelerometerTemperatureReading,bmxGyroscopeXReading,bmxGyroscopeYReading,bmxGyroscopeZReading,bmxMagnetometerXReading,bmxMagnetometerYReading,bmxMagnetometerZReading,bmxMagnetometerHallReading] = RsxImportFile(filename, startRow, endRow)
%IMPORTFILE Import numeric data from a text file as column vectors.
%   [TIMESTAMP,PORTAD,PORTBDIR,PORTAOUT,PORTBOUT,ADCVOLTAGEREADING,ADCCURRENTREADING,ADXLACCELEROMETERXREADING,ADXLACCELEROMETERYREADING,ADXLACCELEROMETERZREADING,MAXIMTEMPERATUREREADING,BMXACCELEROMETERXREADING,BMXACCELEROMETERYREADING,BMXACCELEROMETERZREADING,BMXACCELEROMETERTEMPERATUREREADING,BMXGYROSCOPEXREADING,BMXGYROSCOPEYREADING,BMXGYROSCOPEZREADING,BMXMAGNETOMETERXREADING,BMXMAGNETOMETERYREADING,BMXMAGNETOMETERZREADING,BMXMAGNETOMETERHALLREADING]
%   = IMPORTFILE(FILENAME) Reads data from text file FILENAME for the
%   default selection.
%
%   [TIMESTAMP,PORTAD,PORTBDIR,PORTAOUT,PORTBOUT,ADCVOLTAGEREADING,ADCCURRENTREADING,ADXLACCELEROMETERXREADING,ADXLACCELEROMETERYREADING,ADXLACCELEROMETERZREADING,MAXIMTEMPERATUREREADING,BMXACCELEROMETERXREADING,BMXACCELEROMETERYREADING,BMXACCELEROMETERZREADING,BMXACCELEROMETERTEMPERATUREREADING,BMXGYROSCOPEXREADING,BMXGYROSCOPEYREADING,BMXGYROSCOPEZREADING,BMXMAGNETOMETERXREADING,BMXMAGNETOMETERYREADING,BMXMAGNETOMETERZREADING,BMXMAGNETOMETERHALLREADING]
%   = IMPORTFILE(FILENAME, STARTROW, ENDROW) Reads data from rows STARTROW
%   through ENDROW of text file FILENAME.
%
% Example:
%   [TimeStamp,portAD,portBDir,portAOut,portBOut,adcVoltageReading,adcCurrentReading,adxlAccelerometerXReading,adxlAccelerometerYReading,adxlAccelerometerZReading,maximTemperatureReading,bmxAccelerometerXReading,bmxAccelerometerYReading,bmxAccelerometerZReading,bmxAccelerometerTemperatureReading,bmxGyroscopeXReading,bmxGyroscopeYReading,bmxGyroscopeZReading,bmxMagnetometerXReading,bmxMagnetometerYReading,bmxMagnetometerZReading,bmxMagnetometerHallReading] = importfile('RSX Datalog 4.csv',1, 69642);
%
%    See also TEXTSCAN.

% Auto-generated by MATLAB on 2017/08/26 11:14:40

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

%% Read columns of data as text:
% 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%[^\n\r]';

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

%% Read columns of data according to the format.
% 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, 'EndOfLine', '\r\n');
for block=2:length(startRow)
    frewind(fileID);
    dataArrayBlock = textscan(fileID, formatSpec, endRow(block)-startRow(block)+1, 'Delimiter', delimiter, 'HeaderLines', startRow(block)-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');
    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 text to numbers.
% Replace non-numeric text 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=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]
    % Converts text in the input cell array to numbers. Replaced non-numeric
    % text 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 text 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


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

%% Allocate imported array to column variable names
TimeStamp = cell2mat(raw(:, 1));
portAD = cell2mat(raw(:, 2));
portBDir = cell2mat(raw(:, 3));
portAOut = cell2mat(raw(:, 4));
portBOut = cell2mat(raw(:, 5));
adcVoltageReading = cell2mat(raw(:, 6));
adcCurrentReading = cell2mat(raw(:, 7));
adxlAccelerometerXReading = cell2mat(raw(:, 8));
adxlAccelerometerYReading = cell2mat(raw(:, 9));
adxlAccelerometerZReading = cell2mat(raw(:, 10));
maximTemperatureReading = cell2mat(raw(:, 11));
bmxAccelerometerXReading = cell2mat(raw(:, 12));
bmxAccelerometerYReading = cell2mat(raw(:, 13));
bmxAccelerometerZReading = cell2mat(raw(:, 14));
bmxAccelerometerTemperatureReading = cell2mat(raw(:, 15));
bmxGyroscopeXReading = cell2mat(raw(:, 16));
bmxGyroscopeYReading = cell2mat(raw(:, 17));
bmxGyroscopeZReading = cell2mat(raw(:, 18));
bmxMagnetometerXReading = cell2mat(raw(:, 19));
bmxMagnetometerYReading = cell2mat(raw(:, 20));
bmxMagnetometerZReading = cell2mat(raw(:, 21));
bmxMagnetometerHallReading = cell2mat(raw(:, 22));


