csvread — Read numeric data from CSV files in MATLAB and RunMat.

csvread(filename) reads numeric data from comma-separated text files and returns a dense double matrix. Legacy row/column offset handling (including zero-based forms) follows MATLAB and RunMat compatibility behavior.

Syntax

M = csvread(filename)
M = csvread(filename, row, col)
M = csvread(filename, row, col, range)

Inputs

NameTypeRequiredDefaultDescription
filenameStringScalarYesCSV file path.
rowIntegerScalarYesZero-based starting row offset.
colIntegerScalarYesZero-based starting column offset.
rangeAnyYesA1-style range string or numeric range vector.

Returns

NameTypeDescription
MNumericArrayNumeric matrix read from the CSV file.

Errors

IdentifierWhenMessage
Argument list does not match supported csvread call forms.csvread: invalid argument configuration
Row/column offset arguments are invalid.csvread: invalid row/column index
Range argument is malformed or semantically invalid.csvread: invalid range

How csvread works

  • Accepts character vectors or string scalars for the file name. String arrays must contain exactly one element.
  • csvread(filename, row, col) starts reading at the zero-based row row and column col, skipping any data before that offset.
  • csvread(filename, row, col, range) reads only the rectangle described by range. Numeric ranges must contain four elements [r1 c1 r2 c2] (zero-based, inclusive). Excel-style ranges use the familiar "B2:D6" A1 notation, which RunMat converts to zero-based indices internally.
  • Empty fields (two consecutive commas or a trailing comma) are interpreted as 0. Tokens such as NaN, Inf, and -Inf are accepted (case-insensitive).
  • Any other nonnumeric token raises an error that identifies the offending row and column.
  • File input is read as byte-oriented CSV text before numeric parsing, so non-UTF-8 bytes in skipped header rows or skipped label columns do not cause an I/O decoding failure.
  • Results are dense double-precision tensors using column-major layout. An empty file produces a 0×0 tensor.
  • Paths can contain ~ to reference the home directory; RunMat expands the token before opening the file.

Does RunMat run csvread on the GPU?

csvread performs all work on the host CPU. Arguments are gathered from the GPU when necessary, and the resulting tensor is returned in host memory. To keep data on the GPU, call gpuArray on the output or switch to readmatrix with the 'Like' option. No provider hooks are required.

GPU memory and residency

csvread always returns a host-resident tensor because it performs file I/O and parsing on the CPU. If you need the data on the GPU, wrap the call with gpuArray(csvread(...)) or switch to readmatrix with the 'Like' option so that RunMat can place the result directly on the desired device.

Examples

Import Entire CSV File

writematrix([1 2 3; 4 5 6], "scores.csv");
M = csvread("scores.csv");
disp(M);
delete("scores.csv")

Expected output:

M =
     1     2     3
     4     5     6

Skip Header Row And Column Using Zero-Based Offsets

fid = fopen("with_header.csv", "w");
fprintf(fid, "Name,Jan,Feb\nalpha,1,2\nbeta,3,4\n");
fclose(fid);

M = csvread("with_header.csv", 1, 1);
disp(M);
delete("with_header.csv");

Expected output:

M =
     1     2
     3     4

Read A Specific Range With Numeric Vector Syntax

fid = fopen("measurements.csv", "w");
fprintf(fid, "10,11,12,13\n14,15,16,17\n18,19,20,21\n22,23,24,25\n");
fclose(fid);

block = csvread("measurements.csv", 0, 0, [1 1 2 3])
delete("measurements.csv");

Expected output:

block =
    15    16    17
    19    20    21

Read A Block Using Excel-Style Range Notation

fid = fopen("measurements2.csv", "w");
fprintf(fid, "10,11,12\n14,15,16\n18,19,20\n");
fclose(fid);

sub = csvread("measurements2.csv", 0, 0, "B2:C3")
delete("measurements2.csv");

Expected output:

sub =
    15    16
    19    20

Handle Empty Fields As Zeros

fid = fopen("with_blanks.csv", "w");
fprintf(fid, "1,,3\n,5,\n7,8,\n");
fclose(fid);

M = csvread("with_blanks.csv")
delete("with_blanks.csv");

Expected output:

M =
     1     0     3
     0     5     0
     7     8     0

Read Numeric Data From A File In The Home Directory

homePath = fullfile(getenv("HOME"), "runmat_csvread_home.csv");
fid = fopen(homePath, "w");
fprintf(fid, "9,10\n11,12\n");
fclose(fid);

M = csvread(fullfile("~", "runmat_csvread_home.csv"), 0, 0);
disp(M);
delete(homePath);

Expected output:

M =
     9    10
    11    12

Detect Errors When Text Appears In Numeric Columns

fid = fopen("bad.csv", "w");
fprintf(fid, "1,2,3\n4,error,6\n");
fclose(fid);

try
    csvread("bad.csv");
catch err
    disp(err.message);
end
delete("bad.csv");

Expected output:

csvread: nonnumeric token error at row 2 column 2

Using csvread with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how csvread changes the result.

Run a small csvread example, explain the result, then change one input and compare the output.

FAQ

Why does csvread complain about text data?

csvread is limited to numeric CSV content. If a field contains letters, quoted strings, or other tokens that cannot be parsed as numbers, the builtin raises an error. Switch to readmatrix or readtable when the file mixes text and numbers.

Are the row and column offsets zero-based?

Yes. csvread(filename, row, col) treats row and col as zero-based counts to skip from the start of the file before reading results.

How are Excel-style ranges interpreted?

Excel ranges such as "B2:D5" use the familiar 1-based row numbering and column letters. The builtin converts them internally to zero-based indices and includes both endpoints.

Can I read files with quoted numeric fields?

Quoted numeric fields are not supported. Remove quotes before calling csvread, or switch to readmatrix, which has full CSV parsing support.

What happens to empty cells?

Empty cells (two consecutive commas or a trailing delimiter) become zero, matching MATLAB's csvread behaviour.

Does csvread support custom delimiters?

No. csvread always uses comma separation. Use dlmread or readmatrix for other delimiters.

How do I keep the results on the GPU?

csvread returns a host tensor. Call gpuArray(csvread(...)) after reading, or prefer readmatrix with 'Like', gpuArray.zeros(1) to keep residency on the GPU automatically.

What if the file is empty?

An empty file results in a 0×0 double tensor. MATLAB behaves the same way.

Does csvread change the working directory?

No. Relative paths are resolved against the current working directory and do not modify it.

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how csvread is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.