csvWrite
Write comma-separated value file
Syntax
csvWrite(M, filename) csvWrite(M, filename, separator) csvWrite(M, filename, separator, decimal) csvWrite(M, filename, separator, decimal, precision) csvWrite(M, filename, separator, decimal, precision, header)
Arguments
- filename
a string: the file path.
- M
a m-by-n matrix of strings or double (complex supported).
- separator
a string: the column separator mark.
- decimal
a string: the decimal mark, either "." or ",".
- precision
a string: the C format.
- header
a vector of text: the header written as a column of text at the beginning of the file, before actual data.
Description
This function writes matrix M into filename as comma-separated values.
The default value of the optional input arguments are defined by the
csvDefault
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.
If relevant (ie with 'special' characters), the file will be saved as UTF-8.
Examples
In the following example, we combine the
csvWrite
and csvRead
functions.
// Save a matrix as csv file format M = [1:10] * 0.1; filename = fullfile(TMPDIR, "data.csv"); csvWrite(M, filename); // Read as text mgetl(filename) r = csvRead(filename);
In the following example, we use various options of the
csvWrite
function.
// Save a matrix as csv file format M = rand(2,3); filename = fullfile(TMPDIR, "data.csv"); // // Use tabs as the separator csvWrite(M, filename,ascii(9)); mgetl(filename) // // Use the "," as the decimal point // (and blank space as the separator). csvWrite(M, filename," ",","); mgetl(filename) // // Configure the precision. // Caution: this lower precision may generate // errors in a write-read cycle! csvWrite(M, filename,[],[],"%.8e"); mgetl(filename) // // Configure the header header = [ "// Copyright (C) INRIA" ]; csvWrite(M, filename, [], [],[], header); mgetl(filename)
The following examples are more advanced uses of the
csvWrite
function.
See also
- csvRead — Read comma-separated value file
- csvTextScan — Converts into a matrix texts representing separated values
- csvDefault — Get or set defaults behavior for csv files.
History
Version | Description |
5.4.0 | Function introduced. Based on the 'csv_readwrite' module. |
Report an issue | ||
<< csvTextScan | Planilhas | read_csv >> |