function modedata = importfile(filename, dataLines)
%IMPORTFILE Import data from a text file

% Imports data from a .sph file in TICRA format.

%  modedata = IMPORTFILE(FILENAME) reads
%  data from text file FILENAME for the default selection.  Returns the
%  data as a table.
%
%  modedata = IMPORTFILE(FILE, DATALINES)
%  reads data for the specified row interval(s) of text file FILENAME.
%  Specify DATALINES as a positive scalar integer or a N-by-2 array of
%  positive scalar integers for dis-contiguous row intervals.
%
%  Example:
%  AAVS1d5singleelementrev1110MHzFarField1 = importfile("/Users/daviddavidson/Dropbox/CIRA_Engineering_Director_2017-22/SKA_Bridging_and_EPA/EEPs_AAVS2/Matlab_codes_DUng_Dec_2023_DBD/110MHz_coarse_mesh_sph_files/AAVS1d5_single_element_rev1_110MHz_FarField1(0).sph", [3, Inf]);
%
%  See also READTABLE.
%
% Auto-generated by MATLAB on 22-Feb-2024 13:20:08 with additional comments
% by D B Davidson

%% Input handling

% If dataLines is not specified, define defaults
if nargin < 2
    dataLines = [3, Inf];
end

%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 5);

% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = " ";

% Specify column names and types
opts.VariableNames = ["TICRA", "SWE", "file", "export", "Var5"];
opts.SelectedVariableNames = ["TICRA", "SWE", "file", "export"];
opts.VariableTypes = ["double", "double", "double", "double", "string"];

% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
opts.ConsecutiveDelimitersRule = "join";
opts.LeadingDelimitersRule = "ignore";

% Specify variable properties
opts = setvaropts(opts, "Var5", "WhitespaceRule", "preserve");
opts = setvaropts(opts, "Var5", "EmptyFieldRule", "auto");
opts = setvaropts(opts, "TICRA", "TrimNonNumeric", true);
opts = setvaropts(opts, "TICRA", "ThousandsSeparator", ",");

% Import the data
modedata = readtable(filename, opts);

end