detectImportOptions
Guesses how to read a data file and returns import options.
Category: Language Fundamentals > Data Types
Syntax
opts = detectImportOptions(filename)
Inspects the file and returns an options object.
Description
The function reads the start of the file and decides the delimiter, whether the first line holds variable names, and a type for each column (numbers become double, text becomes char). You can edit the properties, for example change a type or select some variables, and then pass the object to readtable or readmatrix.
This lets you adjust the reading without writing the options by hand.
Input arguments
filename- Path of a delimited text file such as
.csv.
Output arguments
opts- Options object with properties such as
Delimiter,VariableNames,VariableTypesandDataLines.
Example
Detect and read
fid = fopen('demo.csv', 'w');
fprintf(fid, 'a,b\n1,x\n2,y\n');
fclose(fid);
opts = detectImportOptions('demo.csv');
opts.VariableNames
opts.VariableTypes
T = readtable('demo.csv', opts)
Output
ans = 1×2 cell
{'a'} {'b'}
ans = 1×2 cell
{'double'} {'char'}
T = 2×2 table
a b
_ _____
1 {'x'}
2 {'y'}
The example first writes a small file in the current folder.