<< Overview dataINT DI_readxls >>

dataINT >> dataINT > DI_readcsv

DI_readcsv

Imports a CSV file (comma-separated-value) in a matrix variable interactively

Calling Sequence

[csvMat] = DI_readcsv()
[csvMat] = DI_readcsv(path)
[csvMat, exitID] = DI_readcsv()
[csvMat, exitID] = DI_readcsv(path)

Parameters

path:

a string, target path for the file selector (OPTIONAL)

csvMat:

a string, name of the matrix which stores the imported data

exitID:

an integer, exit codes, 0=OK, -1, -2, -3, -4=error codes, see below.

Description

Read data from a comma-separated-value (*.csv) or another text-based data file (*.dat, *.txt) and stores it into a matrix variable interactively.

Note that the file has to be correctly formated. All rows have to have the same number of columns.

path:

You can commit an optional path to the function. This is used to open the file selector at the committed target path. If you omit it your home directory is set as the target path.

csvMat:

This is the name of the matrix variable which will content the imported data for further processing in Scilab's console or in a script.

exitID:

The exitID gives a feedback what happened inside the function. If something went wrong csvMat is always [] (empty). To handle errors in a script you can evaluate exitID's error codes (negative numbers):

  • 0: Everything is OK. Matrix csvMat was created

  • -1: User canceled file selection

  • -2: User canceled parameter dialog box

  • -3: Cannot read or interpret CSV file

  • -4: Cannot interpret file correctly - maybe header present

Import Parameter:

Field Separator:

This is the character which separates the fields and numbers, resp. In general CSV-files it is the comma (,), in European ones it is often the semicolon (;). Sometimes it is a tabulator (tab). Text-based data files has a space or spaces as delimiters. E.g. to specify a tabulator as separator, type in the word "tab", for a space separator the word "space" without quotes.

Decimal separator:

The character which indentifies the decimal place. In general CSV files it is the point (.), in most European ones it is the comma (,).

Number of Header Lines to Skip:

The number of lines to be ignored at the beginning of the file. This is useful to skip table headers.

Row Range:

The rows you want to select for import. E.g. "2:5" imports rows 2, 3, 4 and 5. "2:$" starts the import at row 2 and imports all following rows till the last row is reached. ":" means all rows.

Column Range:

The columns you want to select for import. Refer the description of "row range" above for details.

With row and column range you can import a subset of your raw data table for further processing.

For CSV and text-based data files there are different prerequisites to consider.

Comma-separated-value (*.csv):

All rows have to have the same number of columns. That does not mean that every cell has to be occupied by a number. Only the separator has to be present:

2.3 , 4.4 , 7.6 , 9.5
5.6 , 6.9 ,     ,

Strings or missing data (see above) are represented as Nan (not-a-number) in the matrix. E.g.:

2.3  4.4  7.6  9.5
5.6  6.9  Nan  Nan

Text-based data (*.dat, *.txt):

All rows have to have the same number of columns and should not contain strings (text). Otherwise the data will be imported as a string matrix and not a number matrix.

| 2.3  4.4  7.6  9.5 |
|                    |
| 5.6  6.9           |

Examples

[mat, id] = DI_readcsv(fullfile(DI_getpath(), "demos")); // Read CSV file
disp("Exit-Code: "+string(id),mat,"data:") // Displays imported data "mat" and exit code "id"

See also

Authors


Report an issue
<< Overview dataINT DI_readxls >>