Write comma-separated value file
csv_write(M, filename) csv_write(M, filename, separator) csv_write(M, filename, separator, decimal) csv_write(M, filename, separator, decimal, precision) csv_write(M, filename, separator, decimal, precision, comments)
a 1-by-1 matrix of strings, the file path.
a m-by-n matrix of strings or double (complex supported).
a 1-by-1 matrix of strings, the column separator mark.
a 1-by-1 matrix of strings, the decimal mark. The available values are "." or ",".
a 1-by-1 matrix of strings, the C format.
a m-by-1 matrix of strings, the comments stored at the beginning of the file. This option may be used, for example, to put a licence header in a data file.
This function writes matrix M into filename as comma-separated values.
The default value of the optional input arguments are
defined by the csv_default
function.
Any optional input argument equal to the empty matrix
[]
is set to its default value.
If the file filename
already exists, it is overwritten.
In the following example, we combine the csv_write
and csv_read
functions.
// Save a matrix as csv file format M = [1:10] * 0.1; filename = fullfile(TMPDIR, "datas.csv"); csv_write(M, filename); // Read as text mgetl(filename) r = csv_read(filename); | ![]() | ![]() |
In the following example, we use various options of the
csv_write
function.
// Save a matrix as csv file format M = rand(2,3); filename = fullfile(TMPDIR, "datas.csv"); // // Use tabs as the separator csv_write(M, filename,ascii(9)); mgetl(filename) // // Use the "," as the decimal point // (and blank space as the separator). csv_write(M, filename," ",","); mgetl(filename) // // Configure the precision. // Caution: this lower precision may generate // errors in a write-read cycle! csv_write(M, filename,[],[],"%.8e"); mgetl(filename) // // Configure the comments comments = [ "// Copyright (C) DIGITEO" "// This file must be used under the terms of the CeCILL." ]; csv_write(M, filename,[],[],[],comments); mgetl(filename) | ![]() | ![]() |
The following examples are more advanced uses of the
csv_write
function.
Copyright (C) 2010-2011 - DIGITEO - Allan CORNET
Copyright (C) 2011 - DIGITEO - Michael Baudin