Read comma-separated value file
M = csv_read(filename) M = csv_read(filename, separator) M = csv_read(filename, separator, decimal) M = csv_read(filename, separator, decimal, conversion) M = csv_read(filename, separator, decimal, conversion, substitute) M = csv_read(filename, separator, decimal, conversion, substitute, rexgepcomments, range) [M, comments] = csv_read(filename, separator, decimal, conversion, substitute, rexgepcomments, range)
a 1-by-1 matrix of strings, the file path.
a 1-by-1 matrix of strings, the field separator used.
a 1-by-1 matrix of strings, the decimal used.
a 1-by-1 matrix of strings, the type of the output M
.
Available values are "string" or "double" (default).
a m-by-2 matrix of strings, a replacing map (default = [], meaning no
replacements).
The first column substitute(:,1)
contains the
searched strings and the second column substitute(:,2)
contains the replace strings.
Every occurence of a searched string in the file is replaced.
a string: a regexp to remove lines which match. (default: [])
a 1-by-4 matrix of floating point integers, the range
of rows and columns which must be read (default range=[], meaning
that all the rows and columns).
Specify range using the format [R1 C1 R2 C2]
where (R1,C1) is the upper left corner of the data to
be read and (R2,C2) is the lower right corner.
a m-by-n matrix of strings or double.
a m-by-n matrix of strings matched by regexp.
Given an ascii file with comma separated values delimited fields, this function returns the corresponding Scilab matrix of strings or doubles.
For example, the .csv data file may have been created by a spreadsheet software using "Text and comma" format.
It might happen that the columns are separated by a non-comma separator. In this case, use csv_read(filename, separator) for another choice of separator.
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.
When the input argument "conversion" is equal to "double", the non-numeric fields within the .csv (e.g. strings) are converted into NaN.
The following script presents some basic uses of the
csv_read
function.
// Create a file with some data separated with tabs. M = 1:50; filename = fullfile(TMPDIR, "datas.csv"); csv_write(M, filename, ascii(9), '.'); // read csv file M1 = csv_read(filename,ascii(9), [], 'string') // Returns a double M2 = csv_read(filename,ascii(9), '.', 'double') // Compares original data and result. and(M == M2) // Use the substitude argument to manage // special data files. content = [ "1" "Not-A-Number" "2" "Not-A-Number" ]; substitute = [ "Not-A-Number" "Nan" ]; mputl(content,filename); M = csv_read(filename,",",".","double",substitute) isnan(M(2,1)) // Expected=%t isnan(M(4,1)) // Expected=%t | ![]() | ![]() |
The following script presents more practical uses of the
csv_read
function.
// Define a matrix of strings Astr = [ "1" "8" "15" "22" "29" "36" "43" "50" "2" "9" "16" "23" "30" "37" "44" "51" "3" "10" "17" "6+3*I" "31" "38" "45" "52" "4" "11" "18" "25" "32" "39" "46" "53" "5" "12" "19" "26" "33" "40" "47" "54" "6" "13" "20" "27" "34" "41" "48" "55" "+0" "-0" "Inf" "-Inf" "Nan" "1.D+308" "1.e-308" "1.e-323" ]; // Create a file with some data separated with commas filename = fullfile(TMPDIR , 'foo.csv'); sep = ","; fd = mopen(filename,'wt'); for i = 1 : size(Astr,"r") mfprintf(fd,"%s\n",strcat(Astr(i,:),sep)); end mclose(fd); // To see the file : edit(filename) // Read this file Bstr = csv_read ( filename ) // Create a file with a particular separator: here ";" filename = fullfile(TMPDIR , 'foo.csv'); sep = ";"; fd = mopen(filename,'wt'); for i = 1 : size(Astr,"r") mfprintf(fd,"%s\n",strcat(Astr(i,:),sep)); end mclose(fd); // // Read the file and customize the separator csv_read ( filename , sep ) | ![]() | ![]() |
The following script shows how to remove lines with regexp argument of the
csv_read
function.
Copyright (C) 2010-2011 - DIGITEO - Allan CORNET
Copyright (C) 2011 - DIGITEO - Michael Baudin