FUNCTION REFERENCE
readtable
Read a delimited text file or a spreadsheet into a table.
Read a delimited text file or a spreadsheet into a table.
Syntax
T = readtable(filename)Syntax
T = readtable(filename, 'Delimiter', d, 'ReadVariableNames', tf, 'NumHeaderLines', n, 'TextType', type)Syntax
T = readtable(filename, opts)Syntax
T = readtable(filename, opts, 'ReadVariableNames', tf)Syntax
T = readtable(filename, 'Range', r, 'Sheet', s, 'DataLines', lines, 'VariableNamesLine', k, 'SelectedVariableNames', names, 'DatetimeType', type, 'TreatAsMissing', texts, 'MissingRule', rule)With import options from detectImportOptions (or with any option beyond Delimiter, ReadVariableNames, NumHeaderLines and TextType, or for a spreadsheet), the file is read as those options say: the selected variables in the order of SelectedVariableNames, each converted to its type (double, single, integer, logical, char, string, datetime, duration, categorical), the data lines (DataLines) or cells (Sheet, DataRange), variable units from VariableUnitsLine, row names from RowNamesColumn, empty fields and TreatAsMissing texts as missing, MissingRule and ImportErrorRule ('fill' with the FillValue, 'error', 'omitrow', 'omitvar'), and extra fields per ExtraColumnsRule. Without options the detection of detectImportOptions is used (dates, durations and decimal commas are recognized there).
Without those options the file is read as readcell reads it (delimiter detection, quoted fields, empty fields; blank lines are skipped). After NumHeaderLines lines, the first line holds the variable names (unless 'ReadVariableNames', false gives Var1, Var2, ...); names are made valid: blanks are removed and the next letter capitalized, other invalid characters become '_', a name not starting with a letter gets an 'x' prefix, an empty name is Var<k>, and later duplicates get _1, _2, ... (a name equal to 'Properties', 'Row' or 'Variables' is renamed the same way: Row_1). A column of numbers and empty fields becomes a double variable (empty is NaN); a column with only empty fields, and every column of a file with no data lines, is double too (as in MATLAB). Any other column a cellstr variable ('' for empty fields), or a string variable with 'TextType', 'string' (<missing> for empty fields).
Example
writetable(table([1; 2], {'a'; 'b'}, 'VariableNames', {'x', 'y'}), 'parts.csv'); T = readtable('parts.csv') % 2x2 tableDifferences from MATLAB: readtable(filename) with only the original four options keeps its earlier rules: dates and times stay text, quoted numbers stay text, and numbers in a text column are shown as numbers print. Text variables (cellstr) hold at most 65536 rows (use 'TextType', 'string' for more). Variable names must be valid identifiers, so 'VariableNamingRule', 'preserve' names are made valid in the table. No Format option, 'ExtraColumnsRule', 'wrap' or named ranges; spreadsheet dates stored as numbers read as Excel serial dates only in datetime variables.
These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.