FUNCTION REFERENCE

detectImportOptions

Detect how to import a delimited text file or a spreadsheet.

Detect how to import a delimited text file or a spreadsheet.

Syntax

opts = detectImportOptions(filename)

Syntax

opts = detectImportOptions(filename, 'FileType', type, 'Delimiter', d, 'NumHeaderLines', n, 'ReadVariableNames', tf)

Syntax

opts = detectImportOptions(filename, 'Sheet', s, 'Range', r, 'VariableNamingRule', rule, 'TextType', type, 'DatetimeType', type)

Returns a matlab.io.text.DelimitedTextImportOptions object for text files and a matlab.io.spreadsheet.SpreadsheetImportOptions object for .xlsx, .xlsm, .xlsb and .xls files (or 'FileType', 'spreadsheet'). For text files, delimiter detection picks the comma, tab, semicolon or bar that gives the most sampled lines the same number of fields. Space is the fallback when none of those delimiters occurs. Delimiter also accepts ' ', 'space', 'whitespace' or a list of delimiters such as {' ', '\t'}. When space is a delimiter, runs of spaces and tabs are joined by default and leading and trailing whitespace is ignored. Detection also finds header lines (lines before the first line with that many fields), the variable-names line (a first line holding only text), a units line (a text line after the names over numeric data), the first data line, and each column's type: 'double' (numbers, Inf, NaN; also numbers with a decimal comma, which set DecimalSeparator ','; a column of mostly numbers whose other texts become import errors), 'datetime' (ISO forms such as 2024-01-15 or 2024-01-15T08:30:00, MM/dd/yyyy, dd/MM/yyyy when a day exceeds 12, dd.MM.yyyy, dd-MMM-yyyy), 'duration' (hh:mm:ss), otherwise text ('char', or 'string' with 'TextType', 'string'); categorical is never detected. Read and change the result with dot names (opts.SelectedVariableNames = {...}, opts.DataLines = [3 Inf], opts.VariableNames{2} = 'T'); options objects are values: changing a copy leaves the original as it was. Also accepted: 'DecimalSeparator', 'ThousandsSeparator', 'TreatAsMissing', 'MissingRule', 'ImportErrorRule', 'EmptyLineRule', 'ExtraColumnsRule', 'Whitespace', 'CommentStyle', 'VariableNamesLine', 'VariableUnitsLine', 'DataLines', 'SelectedVariableNames', 'RowNamesColumn', 'ReadRowNames' and the three DelimitersRule options.

Example

writetable(table([1; 2], {'a'; 'b'}, 'VariableNames', {'x', 'y'}), 'parts.csv'); opts = detectImportOptions('parts.csv'); opts.VariableTypes   % {'double'  'char'}

Example

writetable(table([1; 2], 'VariableNames', {'n'}), 'one.xlsx'); opts = detectImportOptions('one.xlsx'); opts.DataRange   % 'A2'

Differences from MATLAB: detection rules are this program's own (they agree on common files); decimal commas are detected; the Encoding is always UTF-8; only English month names; 'VariableNamingRule', 'preserve' names are kept in the options but made valid when a table is built. VariableOptions is a struct array of option objects (class struct; each element has its own class).

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.