csvread

Read comma-separated value (CSV) file

Syntax

  • M = csvread(filename) example
  • M = csvread(filename,R1,C1) example
  • M = csvread(filename,R1,C1,[R1 C1 R2 C2]) example

Description

example

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].

Examples

expand all

Read Entire CSV File

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 CSV File Starting at Specific Row and Column Offset

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 Specific Range from CSV File

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

expand all

filename — File namestring

File name, specified as a string.

Example: 'myFile.dat'

Data Types: char

R1 — Starting row offset0 (default) | nonnegative integer

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

C1 — Starting column offset0 (default) | nonnegative integer

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

R2 — Ending row offsetnonnegative integer

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

C2 — Ending column offsetnonnegative integer

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

More About

expand all

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.

Was this topic helpful?