Scilab Home page | Wiki | Bug tracker | Forge | Mailing list archives | ATOMS | File exchange
Please login or create an account
Change language to: Français - Português - 日本語 - Русский

Please note that the recommended version of Scilab is 6.1.1. This page might be outdated.
However, this page did not exist in the previous stable version.

Scilab help >> Spreadsheet > csvWrite


Write comma-separated value file

Calling Sequence

csvWrite(M, filename)
csvWrite(M, filename, separator)
csvWrite(M, filename, separator, decimal)
csvWrite(M, filename, separator, decimal, precision)
csvWrite(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 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.


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

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));
// Use the "," as the decimal point
// (and blank space as the separator).
csvWrite(M, filename," ",",");
// Configure the precision.
// Caution: this lower precision may generate 
// errors in a write-read cycle!
csvWrite(M, filename,[],[],"%.8e");
// Configure the comments
comments = [
"// Copyright (C) INRIA"
"//  This file must be used under the terms of the CeCILL."
csvWrite(M, filename,[],[],[],comments);

The following examples are more advanced uses of the csvWrite function.

A = [
1 0 200 %inf 0
1 1.e-300 200 %inf 0
9.99999999999990010e-001 9.99999999999999980e-201 200 3.15e300 102
9.99999999899999990e-001 1.e-100 200 296 117
1 %inf -%inf %nan 0

// Write into a file
csvWrite ( A , filename );

See Also


5.4.0 Function introduced. Based on the 'csv_readwrite' module.


Author : Saulo Barbosa Oliveira posted the 08/04/2013 09:04
For some reason all the files created here with csvWrite have a empty line between each of two lines of the matrix..

the write_csv was fine in the Scilab v5.3.3, i'm going back to this version.
Reply to this comment
Please login to comment this page
Author : Saulo Barbosa Oliveira posted the 08/04/2013 09:07
I'm running Windows 8 64bits and for fix this problem i have to set the eol for linux...
Reply to this comment
Please login to comment this page

Author : Anoop Sasidharan posted the 12/09/2013 08:43
where is the .csv file saved? Is it in our current working directory??
Reply to this comment
Please login to comment this page

Add a comment:
Please login to comment this page.

Report an issue
<< csvTextScan Spreadsheet read_csv >>

Scilab Enterprises
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Apr 02 17:36:24 CEST 2013