table2csv

Writes a table object t to a comma separated values (.csv) file f.

Syntax

table2csv(f, t)

table2csv(f, t, '-append')

table2csv(f, t, property, value, ...)

Inputs

f
File name or file handle. If the file exists, it will be overwritten unless the property -append or append and on are specified.
Type: string | int
t
Table object.
Type: table
-append
Optional keyword that appends the table object t to an existing file f.
Type: string
property, value
append
Optional property that specifies if the file f should be overwritten or appended. Valid values are off (default) or on.
Type: string
delimiter
Optional property that specifies the column delimiter, the default value being ','.
Type: string
newline
Optional property that specifies the row delimiter. Apart from any non-empty string, valid values are \n (default), unix ('\n'), pc ('\r\n') or mac ('\r').
Type: string
precision
Specifies the precision format used by 'fprintf' to write numerical data. The default will be the format used in the current session of the application>.
Type: string

Examples

Default properties with file descriptor:
fopen_fileID = fopen('table2csv_example.csv', 'w');
Lake = {'Superior','Michigan','Huron','Erie','Ontario'};
Area = [31700, 22300, 23000, 9910, 7340];
Volume = [2900, 1180, 850, 116, 393];
t = table(Lake, Area, Volume);
table2csv(fopen_fileID, t);
fclose(fopen_fileID);
Append and precision properties:
filename = 'table2csv4.csv';
Lake = {'Superior','Michigan','Huron','Erie','Ontario'};
Area = [31700, 22300, 23000, 9910, 7340];
Volume = [2900, 1180, 850, 116, 393];
t = table(Lake, Area, Volume);
table2csv(filename, t);
Data2 = [pi, 100.23 + i, 850, 116, 393];
t2 = table(Lake, Data2);
table2csv(filename, t2, 'append', 'on', 'precision', '%.2E')
disp(fileread(filename))
Lake,Area,Volume
Superior,31700,2900
Michigan,22300,1180
Huron,23000,850
Erie,9910,116
Ontario,7340,393
Lake,Data2
Superior,3.14E+00
Michigan,1.00E+02 + 1.00E+00i
Huron,8.50E+02
Erie,1.16E+02
Ontario,3.93E+02
Delimiter and newline properties:
filename = 'table2csv3.csv';
Lake = {'Superior','Michigan','Huron','Erie','Ontario'};
Area = [31700, 22300, 23000, 9910, 7340];
Volume = [2900, 1180, 850, 116, 393];
t = table(Lake, Area, Volume);
table2csv(filename, t, 'newline', strcat('$$', char(10)), 'delimiter', '---')
disp(fileread(filename))
Lake---Area---Volume$$
Superior---31700---2900$$
Michigan---22300---1180$$
Huron---23000---850$$
Erie---9910---116$$
Ontario---7340---393$$