writetable

Writes a table to a delimited text file.

Category: Language Fundamentals > Data Types

Syntax

writetable(T, filename)

Writes T to filename; the extension (.csv, .txt, .xlsx) chooses the format.

writetable(T, filename, Name, Value)

Adjusts the output with options such as 'WriteVariableNames'.

Description

Each table row becomes a line of the file, with values separated by commas for .csv. The first line holds the variable names unless you turn that off. Numbers are written in text form and text variables are written as they are.

An existing file with the same name is overwritten. Read the file back with readtable.

Input arguments

T
The table to write.
filename
Output file name, as text.
'WriteVariableNames'
Logical. When false, the header line with variable names is omitted.

Example

Write and read back

T = table([1;2], {'a';'b'}, 'VariableNames', {'n', 's'});
writetable(T, 'out.csv');
R = readtable('out.csv')
Output
R = 2×2 table

    n      s  
    _    _____

    1    {'a'}
    2    {'b'}

Notes

The 'Delimiter' and 'WriteRowNames' options are not applied in MatLivre: files are always comma separated and row names are not written.

See also

readtable writetimetable writematrix table