summaryrefslogtreecommitdiff
path: root/modules/spreadsheet/help/en_US/csvWrite.xml
blob: 03bece61f0e7b02a9af9c77771e2444a3ca39e59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Copyright (C) 2010-2011 - INRIA - Allan CORNET
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 -->
<refentry version="5.0-subset Scilab" xml:id="csvWrite" xml:lang="en"
        xmlns="http://docbook.org/ns/docbook"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        xmlns:svg="http://www.w3.org/2000/svg"
        xmlns:ns3="http://www.w3.org/1999/xhtml"
        xmlns:mml="http://www.w3.org/1998/Math/MathML"
        xmlns:db="http://docbook.org/ns/docbook">
    <refnamediv>
        <refname>csvWrite</refname>
        <refpurpose>Write comma-separated value file</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Calling Sequence</title>
        <synopsis>
            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)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>filename</term>
                <listitem>
                    <para>a 1-by-1 matrix of strings, the file path.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>M</term>
                <listitem>
                    <para>a m-by-n matrix of strings or double (complex
                        supported).
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>separator</term>
                <listitem>
                    <para>a 1-by-1 matrix of strings, the column separator mark.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>decimal</term>
                <listitem>
                    <para>a 1-by-1 matrix of strings, the decimal mark. The available
                        values are "." or ",".
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>precision</term>
                <listitem>
                    <para>a 1-by-1 matrix of strings, the C format.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>comments</term>
                <listitem>
                    <para>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.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>This function writes matrix M into filename as comma-separated
            values.
        </para>
        <para>The default value of the optional input arguments are defined by the
            <literal>csvDefault</literal> function.
        </para>
        <para>Any optional input argument equal to the empty matrix
            <literal>[]</literal> is set to its default value.
        </para>
        <para>
            If the file <literal>filename</literal> already exists, it is
            overwritten.
        </para>
        <para>
            If relevant (ie with 'special' characters), the file will be saved as UTF-8.
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <para>In the following example, we combine the
            <literal>csvWrite</literal> and <literal>csvRead</literal>
            functions.
        </para>
        <programlisting role="example"><![CDATA[// 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);
        ]]></programlisting>
        <para>In the following example, we use various options of the
            <literal>csvWrite</literal> function.
        </para>
        <programlisting role="example"><![CDATA[// 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 comments
comments = [
"// Copyright (C) INRIA"
"//  This file must be used under the terms of the CeCILL."
];
csvWrite(M, filename,[],[],[],comments);
mgetl(filename)
        ]]></programlisting>
        <para>The following examples are more advanced uses of the
            <literal>csvWrite</literal> function.
        </para>
        <programlisting role="example"><![CDATA[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
filename=fullfile(TMPDIR,"foo.csv");
csvWrite ( A , filename );
edit(filename)
    ]]></programlisting>
    </refsection>
    <refsection>
        <title>See Also</title>
        <simplelist type="inline">
            <member>
                <link linkend="csvRead">csvRead</link>
            </member>
        </simplelist>
    </refsection>
    <refsection>
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>5.4.0</revnumber>
                <revremark>Function introduced. Based on the 'csv_readwrite' module.</revremark>
            </revision>
        </revhistory>
    </refsection>
</refentry>