Main Content

csvread

(Not recommended) Read comma-separated value (CSV) file

csvread is not recommended. Use readmatrix instead. For more information, see Compatibility Considerations.

Description

M = csvread(filename) reads a comma-separated value (CSV) formatted file into array M. The file must contain only numeric values.

example

M = csvread(filename,R1,C1) reads data from the file starting at row offset R1 and column offset C1. For example, the offsets R1=0, C1=0 specify the first value in the file.

example

M = csvread(filename,R1,C1,[R1 C1 R2 C2]) reads only the range bounded by row offsets R1 and R2 and column offsets C1 and C2. Another way to define the range is to use spreadsheet notation, such as 'A1..B7' instead of [0 0 6 1].

example

Examples

collapse all

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

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

Read the numeric values in the file.

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

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

Read the matrix starting two rows below the first row from the file described in the previous example.

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

     5    10    15    20
     7    14    21    28

Read the matrix bounded by row offsets 1 and 2 and column offsets 0 and 2 from the file described in the first example.

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

     3     6     9
     5    10    15

Input Arguments

collapse all

File name, specified as a character vector or string.

Example: 'myFile.dat' or "myFile.dat"

Data Types: char | string

Starting row offset, specified as a nonnegative integer. The first row has an offset of 0.

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

Starting column offset, specified as a nonnegative integer. The first column has an offset of 0.

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

Ending row offset, specified as a nonnegative integer. The first row has an offset of 0.

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

Ending column offset, specified as a nonnegative integer. The first column has an offset of 0.

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

Tips

  • Skip header rows or columns by specifying row and column offsets. All values in the file other than headers must be numeric.

Algorithms

csvread fills empty delimited fields with zero. When the csvread function 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.

Version History

Introduced before R2006a

collapse all

R2019a: csvread is not recommended

csvread is not recommended. Use readmatrix instead. There are no plans to remove csvread.

Starting in R2019a, use the readmatrix function to read a matrix from a csv file. The readmatrix function has these advantages over the csvread function:

  • Better cross-platform support and performance

  • Automatic detection of data format and types

  • Ability to use import options to control the data import process, including the handling of errors and missing data

This table shows typical usages of csvread and how to update your code to use readmatrix instead.

Not Recommended

Recommended

M = csvread(filename)
M = readmatrix(filename)