writematrix

Writes a numeric matrix to a delimited text file or spreadsheet.

Category: Data Import and Analysis > Data Import and Export

Syntax

writematrix(A, filename)

Writes A; the format follows the extension (.csv, .txt, .dat, .xlsx).

writematrix(A, filename, 'Delimiter', d)

Uses delimiter d, for example ';' or 'tab'.

writematrix(A, filename, 'WriteMode', 'append')

Adds rows to the end of an existing text file.

Description

Each row of A becomes one line and the elements of the row are separated by the delimiter, which is a comma by default for .csv. Numbers are written with up to 15 significant digits, so 1/3 is stored as 0.333333333333333. The result can be read back with readmatrix.

An existing file is replaced unless 'WriteMode' is 'append'.

Input arguments

A
Numeric matrix.
filename
Output file name.
'Delimiter'
Field separator for text files.
'WriteMode'
'overwrite' (default) or 'append'.

Example

CSV and tab-delimited

A = [1 2; 3 4];
writematrix(A, 'matrix_demo.csv');
writematrix([5 6], 'matrix_demo.csv', 'WriteMode', 'append');
type matrix_demo.csv
writematrix(A, 'matrix_demo.dat', 'Delimiter', 'tab');
readmatrix('matrix_demo.dat')
Output

1,2
3,4
5,6

ans = 2×2

     1     2
     3     4

See also

readmatrix writecell writetable xlswrite