csvread
Reads the values of a 2D matrix, R, from the file, f, where the values are comma-separated.
Syntax
R = csvread(f)
R = csvread(f, ..., row, col)
R = csvread(f, ..., dataRange)
R = csvread(f, ..., 'emptyvalue', value)
Inputs
- f
- File name
- row(optional)
- Specifies the row to read the data from the file, f. row is zero-based. This argument is identical to the optional argument in dlmread.
- col(optional)
- Specifies the column to read the data from the file, f. column is zero-based. This argument is identical to the optional argument in dlmread.
- dataRange(optional)
- Specifies the range of data read. The range can be specified as a 4-element vector in the form [r1, c1, r2, c2]. r1 and c1 are the start row/column the data is read from. r2 and c2 are the end row/column the data is read from. r1, c1, r2, and c2 are zero-based. The range can also be specified as a spreadsheet-style string such as 'A1..B5' or 'A1:B5', where A refers to column 1 of the data and 1 refers to the one-based start row of the data to be read. This argument is identical to the optional argument in dlmread.
- value(optional)
- Argument following the keyword 'emptyvalue', specifying the value to be used in the output, R, for empty or non-numeric values. By default, value is 0. This argument is identical to the optional argument in dlmread.
Outputs
- R
- Matrix of values read from the file, f.
Examples
Default options, reading a comma separated file with numeric data:
R = csvread('Example1.csv');
R = [Matrix] 2 x 3
5.48813 6.45894 7.91725
0.87129 9.78618 6.39921
Start row and column options:
R = csvread('Example2.csv', 2, 1)
R = [Matrix] 3 x 2
6.17636 3.59508
5.92845 -3.84382
6.48172 4.73608
Reading with row/column range:
R = csvread('Example3.csv', [2, 1, 4, 2])
R = [Matrix] 3 x 2
6.17636 3.59508
5.92845 -3.84382
6.48172 4.73608
Reading with string range, option 1:
R = csvread('Example4.csv', 'B1:D2')
R = [Matrix] 2 x 2
6.45894 7.91725
9.78618 6.39921
Reading with string range, option 2:
R = csvread('Example4.csv', 'B1..D2')
R = [Matrix] 2 x 2
6.45894 7.91725
9.78618 6.39921
Reading with empty value specified:
R = csvread('Example5.csv', 'emptyvalue', -55)
R = [Matrix] 3 x 3
5.48813 -55.00000 -55.00000
0.87129 9.78618 6.39921
2.64556 6.17636 3.59508