csvread
Reads a comma-separated numeric file into a matrix.
Category: Data Import and Analysis > Data Import and Export
Syntax
M = csvread(filename)
Reads all numeric data in the file.
M = csvread(filename, R1, C1)
Starts reading at zero-based row R1 and column C1.
Description
csvread is dlmread with the delimiter fixed to a comma. It accepts only numbers, so a file with text headers or text columns should be read with readmatrix or readtable instead.
The offsets are zero-based, unlike MATLAB indexing: csvread(f, 1, 0) skips one header row.
Input arguments
filename- Name of the file.
R1, C1- Row and column offsets, counted from 0, of the first value to read.
Output arguments
M- Numeric matrix. Missing fields are read as
0.
Example
Skip a header row and a column
fid = fopen('d.csv', 'w');
fprintf(fid, 'a,b,c\n1,2,3\n4,5,6\n');
fclose(fid);
csvread('d.csv', 1, 1)
Output
ans = 2×2
2 3
5 6
Notes
MathWorks recommends readmatrix for new code.