1 <?xml version="1.0" encoding="UTF-8"?>
3 * Copyright (C) 2010-2011 - INRIA - Allan CORNET
4 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
6 <refentry version="5.0-subset Scilab" xml:id="csvRead" xml:lang="en"
7 xmlns="http://docbook.org/ns/docbook"
8 xmlns:xlink="http://www.w3.org/1999/xlink"
9 xmlns:svg="http://www.w3.org/2000/svg"
10 xmlns:ns3="http://www.w3.org/1999/xhtml"
11 xmlns:mml="http://www.w3.org/1998/Math/MathML"
12 xmlns:db="http://docbook.org/ns/docbook">
14 <pubdate>$LastChangedDate$</pubdate>
18 <refname>csvRead</refname>
20 <refpurpose>Read comma-separated value file</refpurpose>
24 <title>Calling Sequence</title>
28 M = csvRead(filename, separator)
29 M = csvRead(filename, separator, decimal)
30 M = csvRead(filename, separator, decimal, conversion)
31 M = csvRead(filename, separator, decimal, conversion, substitute)
32 M = csvRead(filename, separator, decimal, conversion, substitute, rexgepcomments, range)
33 [M, comments] = csvRead(filename, separator, decimal, conversion, substitute, rexgepcomments, range)
38 <title>Parameters</title>
45 <para>a 1-by-1 matrix of strings, the file path.</para>
50 <term>separator</term>
53 <para>a 1-by-1 matrix of strings, the field separator used.</para>
61 <para>a 1-by-1 matrix of strings, the decimal used.</para>
66 <term>conversion</term>
69 <para>a 1-by-1 matrix of strings, the type of the output
70 <literal>M</literal>. Available values are "string" or "double" (by default).
73 Note that <link linkend="read_csv">read_csv</link> has "string" as default.
79 <term>substitute</term>
82 <para>a m-by-2 matrix of strings, a replacing map (default = [],
83 meaning no replacements). The first column
84 <literal>substitute(:,1)</literal> contains the searched strings and
85 the second column <literal>substitute(:,2)</literal> contains the
86 replace strings. Every occurence of a searched string in the file is
93 <term>rexgepcomments</term>
96 <para>a string: a regexp to remove lines which match. (default:
106 <para>a 1-by-4 matrix of floating point integers, the range of rows
107 and columns which must be read (default range=[], meaning that all
108 the rows and columns). Specify range using the format <literal>[R1
111 where (R1,C1) is the upper left corner of the
112 data to be read and (R2,C2) is the lower right corner.
121 <para>a m-by-n matrix of strings or double.</para>
126 <term>comments</term>
129 <para>a m-by-n matrix of strings matched by regexp.</para>
136 <title>Description</title>
138 <para>Given an ascii file with comma separated values delimited fields,
139 this function returns the corresponding Scilab matrix of strings or
143 <para>For example, the .csv data file may have been created by a
144 spreadsheet software using "Text and comma" format.
147 <para>It might happen that the columns are separated by a non-comma
148 separator. In this case, use csvRead(filename, separator) for another
152 <para>The default value of the optional input arguments are defined by the
153 <literal>csvDefault</literal> function.
156 <para>Any optional input argument equal to the empty matrix
157 <literal>[]</literal> is set to its default value.
160 <para>When the input argument "conversion" is equal to "double", the
161 non-numeric fields within the .csv (e.g. strings) are converted into
167 <title>Examples</title>
169 <para>The following script presents some basic uses of the
170 <literal>csvRead</literal> function.
173 <programlisting role="example">// Create a file with some data separated with tabs.
175 filename = fullfile(TMPDIR, "data.csv");
176 csvWrite(M, filename, ascii(9), '.');
179 M1 = csvRead(filename,ascii(9), [], 'string')
182 M2 = csvRead(filename,ascii(9), '.', 'double')
184 // Compares original data and result.
187 // Use the substitude argument to manage
188 // special data files.
200 mputl(content,filename);
201 M = csvRead(filename,",",".","double",substitute)
202 isnan(M(2,1)) // Expected=%t
203 isnan(M(4,1)) // Expected=%t
207 <para>The following script presents more practical uses of the
208 <literal>csvRead</literal> function.
211 <programlisting role="example">// Define a matrix of strings
213 "1" "8" "15" "22" "29" "36" "43" "50"
214 "2" "9" "16" "23" "30" "37" "44" "51"
215 "3" "10" "17" "6+3*I" "31" "38" "45" "52"
216 "4" "11" "18" "25" "32" "39" "46" "53"
217 "5" "12" "19" "26" "33" "40" "47" "54"
218 "6" "13" "20" "27" "34" "41" "48" "55"
219 "+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323"
222 // Create a file with some data separated with commas
223 filename = fullfile(TMPDIR , 'foo.csv');
225 fd = mopen(filename,'wt');
226 for i = 1 : size(Astr,"r")
227 mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
230 // To see the file : edit(filename)
233 Bstr = csvRead ( filename )
235 // Create a file with a particular separator: here ";"
236 filename = fullfile(TMPDIR , 'foo.csv');
238 fd = mopen(filename,'wt');
239 for i = 1 : size(Astr,"r")
240 mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
245 // Read the file and customize the separator
246 csvRead ( filename , sep )
249 <para>The following script shows how to remove lines with regexp argument
250 of the <literal>csvRead</literal> function.
253 <programlisting role="example">CSV = ["// tata"; ..
260 filename = fullfile(TMPDIR , 'foo.csv');
261 mputl(CSV, filename);
263 // remove lines with // @ beginning
264 [M, comments] = csvRead(filename, [], [], [], [], '/\/\//')
269 <para>Empty field are managed by csvRead</para>
271 <programlisting role="example">
272 csvWrite(['1','','3';'','','6'], TMPDIR + "/example.csv")
273 csvRead(TMPDIR + "/example.csv", [], [], "string")
274 csvRead(TMPDIR + "/example.csv", [], [], "double")
278 <programlisting role="example">
279 // Define a matrix of strings
281 "1" "8" "15" "22" "29" "36" "43" "50"
282 "2" "9" "16" "23" "30" "37" "44" "51"
283 "3" "10" "17" "6+3*I" "31" "38" "45" "52"
284 "4" "11" "18" "25" "32" "39" "46" "53"
285 "5" "12" "19" "26" "33" "40" "47" "54"
286 "6" "13" "20" "27" "34" "41" "48" "55"
287 "+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323"
290 // Create a file with some data separated with commas
291 filename = fullfile(TMPDIR , 'foo.csv');
293 fd = mopen(filename,'wt');
294 for i = 1 : size(Astr,"r")
295 mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
298 // To see the file : edit(filename)
301 Bstr = csvRead ( filename )
303 // Create a file with a particular separator: here ";"
304 filename = fullfile(TMPDIR , 'foo.csv');
306 fd = mopen(filename,'wt');
307 for i = 1 : size(Astr,"r")
308 mfprintf(fd,"%s\n",strcat(Astr(i,:),sep));
312 // Read the file and customize the separator
313 csvRead ( filename , sep )
317 <para>In the following script, the file "filename" is read by blocks of
318 5000 rows. The algorithm stops when the number of rows actually read from
319 the file differ from 5000, i.e. when the end of the file has been
323 <programlisting role="example">blocksize = 5000;
328 R1 = (iblock-1) * blocksize + 1;
329 R2 = blocksize + R1-1;
330 irange = [R1 C1 R2 C2];
331 mprintf("Block #%d, rows #%d to #%d\n",iblock,R1,R2);
333 M=csvRead(filename , [] , [] , [] , [] , [] , [] , irange );
337 if ( nrows > 0 ) then
338 p = t/(nrows*ncols)*1.e6;
339 mprintf(" Actual #rows=%d\n",nrows);
340 mprintf(" T=%.3f (s)\n",t);
341 mprintf(" T=%.1f (ms/cell)\n",p);
343 if ( nrows < blocksize ) then
344 mprintf("... End of the file.\n");
351 <para>This produces :</para>
353 <programlisting role="no-scilab-exec">Block #1, rows #1 to #5000
357 Block #2, rows #5001 to #10000
361 Block #3, rows #10001 to #15000
370 <title>See Also</title>
372 <simplelist type="inline">
374 <link linkend="csvWrite">csvWrite</link>
379 <title>History</title>
382 <revnumber>5.4.0</revnumber>
384 Function introduced. Based on the 'csv_readwrite' module. The only difference in the behavior compared to <link linkend="read_csv">read_csv</link> is that csvRead will try to convert value to double by default when read_csv will return value as string.