delimitedTextImportOptions

Creates an options object that describes how to import a delimited text file.

Category: Data Import and Analysis > Data Import and Export

Syntax

opts = delimitedTextImportOptions

Returns default options for a delimited file.

opts = delimitedTextImportOptions('NumVariables', n)

Declares the number of columns.

opts = delimitedTextImportOptions(..., Name, Value)

Sets properties such as VariableNames, VariableTypes and Delimiter at creation.

Description

Import options give you full control over a text import: which columns exist, what type each one has, and which are selected. The object can be inspected, changed, and reused across many files with the same layout.

To let the library guess the layout instead, call detectImportOptions on a sample file and adjust only what it got wrong.

Input arguments

'NumVariables'
Number of columns in the file.
'VariableNames'
Cell array of column names for the resulting table.
'VariableTypes'
Cell array of types: 'double', 'string', 'char', 'datetime', and so on.
'Delimiter'
Character or cell array of characters that separate fields.

Output arguments

opts
Import options object, passed as the second argument of readtable, readmatrix or readcell.

Example

Read a headerless file with chosen types

fid = fopen('p.csv', 'w');
fprintf(fid, 'Ann,30\nBob,25\n');
fclose(fid);
opts = delimitedTextImportOptions('NumVariables', 2, ...
    'VariableNames', {'name', 'age'}, ...
    'VariableTypes', {'string', 'double'}, ...
    'Delimiter', ',');
T = readtable('p.csv', opts)
Output
T = 2×2 table

    name     age
    _____    ___

    "Ann"    30 
    "Bob"    25

Notes

In MatLivre, DataLines passed to the constructor is ignored, and assigning VariableNames to the object afterwards (opts.VariableNames = ...) makes readtable fail with an unknown-variable error. Pass names and types as constructor arguments, as in the example, and use header-free data.

See also

detectImportOptions readtable readmatrix readcell