Documentation Center

  • Trial Software
  • Product Updates

csvread

Read comma-separated value file

Syntax

  • M = csvread(filename) example
  • M = csvread(filename,row,col) example
  • M = csvread(filename,row,col,csvRange) example

Description

example

M = csvread(filename) reads a comma-separated value formatted file, filename. The file can only contain numeric values.

example

M = csvread(filename,row,col) reads data from the file starting at the specified row and column. The row and column arguments are zero based, so that row = 0 and col = 0 specify the first value in the file.

example

M = csvread(filename,row,col,csvRange) reads only the range specified by csvRange.

Examples

expand all

Read File Containing Comma-Separated Values

Create a file, csvlist.dat, that contains these comma-separated values.

   02, 04, 06, 08
   03, 06, 09, 12
   05, 10, 15, 20
   07, 14, 21, 28
  

Read the entire file.

filename = 'csvlist.dat';
M = csvread(filename)
M =

     2     4     6     8
     3     6     9    12
     5    10    15    20
     7    14    21    28

csvread returns the numeric data in M.

Read Data Starting at Specific Row and Column

Read data from the file, csvlist.dat, of the previous example, starting at zero-based row 2, column 0.

M = csvread('csvlist.dat',2,0)
M =

     5    10    15    20
     7    14    21    28

Read a Specific Range of Data

Read a specific range of data from the file, csvlist.dat, of the first example.

Read the matrix bounded by zero-based (1,0) and (2,2).

M = csvread('csvlist.dat',1,0,[1,0,2,2])
M =

     3     6     9
     5    10    15

Input Arguments

expand all

filename — Name of file to readstring

Name of the file to read, specified as a string.

Example: 'myFile.dat'

Data Types: char

row — Row of first value to read0 (default) | positive integer

Row of the first value to read, specified as a positive integer. row is zero based, so that row = 0 specifies the first row of data.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

col — Column of first value to read0 (default) | positive integer

Column of the first value to read, specified as a positive integer. col is zero based, so that col = 0 specifies the first column of data.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

csvRange — Range of data to read4-element vector | string

Range of the data to read, specified as a 4-element vector or a string.

  • If csvRange is a 4-element vector, then it must have the form [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. The range is zero based, so that R1 = 0 specifies the first row of data, and C1 = 0 specifies the first column of data.

  • If csvRange is a string, then it should be specified using spreadsheet notation, as in csvRange = 'A1..B7'.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char

Output Arguments

expand all

M — Numeric datamatrix

Numeric data, returned as a matrix of double values.

More About

expand all

Algorithms

csvread fills empty delimited fields with zero. When csvread reads data files with lines that end with a nonspace delimiter, such as a semicolon, it returns a matrix, M, that has an additional last column of zeros.

csvread imports any complex number as a whole into a complex numeric field, converting the real and imaginary parts to the specified numeric type. The table shows valid forms for a complex number.

Form

Example

±<real>±<imag>i|j

5.7-3.1i

±<imag>i|j

-7j

Embedded white-space in a complex number is invalid and is regarded as a field delimiter.

See Also

| | | |

Was this topic helpful?