csvread — Read numeric data from CSV files in MATLAB and RunMat.
csvread(filename) reads real or complex numeric data from comma-separated text files and returns a dense double matrix. Row and column controls are zero-based.
Syntax
M = csvread(filename)
M = csvread(filename, row, col)
M = csvread(filename, row, col, range)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
filename | StringScalar | Yes | — | CSV file path. |
row | IntegerScalar | Yes | — | Zero-based starting row offset. |
col | IntegerScalar | Yes | — | Zero-based starting column offset. |
range | Any | Yes | — | A1-style range string or numeric range vector. |
Returns
| Name | Type | Description |
|---|---|---|
M | NumericArray | Numeric matrix read from the CSV file. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | 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 |
| — | Filename argument is not a scalar string/char vector. | csvread: invalid filename input |
| — | Filename resolves to an empty string. | csvread: filename must not be empty |
| — | Input file cannot be opened. | csvread: unable to open file |
| — | Input file cannot be read. | csvread: failed to read file |
| — | A CSV field cannot be parsed as numeric. | csvread: nonnumeric token encountered |
| — | Internal tensor materialization for csvread output fails. | csvread: unable to construct output matrix |
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 rowrowand columncol, skipping any data before that offset.csvread(filename, row, col, range)reads only the rectangle described byrange. The public forms are a four-element numeric range[r1 c1 r2 c2]or an A1 range such as"B2..D6". RunMat's colon-separated A1 range and two-element numeric range are independent compatibility-gated extensions.- Empty fields (two consecutive commas or a trailing comma) are interpreted as
0. Tokens such asNaN,Inf, and-Infare 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.
- Real input produces a dense double tensor; any complex token (
a+bi,a+bj, or a pure-imaginary form) produces a complex-double tensor without discarding an all-zero imaginary component. An empty file produces a0×0tensor. - 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 file I/O and parsing on the host CPU. Public host controls require no provider. With the csvread-resident-control-inputs extension enabled, resident controls are gathered through their owning provider; the resulting tensor still returns in host memory.
GPU memory and residency
csvread always returns a host-resident tensor because it performs file I/O and parsing on the CPU. Resident filename, offset, and range controls are accepted only when the csvread-resident-control-inputs RunMat extension is enabled.
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 6Skip 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 4Read 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 21Read 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 20Handle 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 0Read 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 12Detect 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 2Using 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?⌄
The public A1 form uses two dots, for example "B2..D5", with 1-based row numbers and column letters. RunMat also accepts colon syntax such as "B2:D5" only when the csvread-colon-range extension is enabled. Both forms include their endpoints.
Can I read files with quoted numeric fields?⌄
Yes. A field wrapped in double quotes is parsed as a numeric token; general quoted-field CSV escaping is outside csvread's narrow numeric format.
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.
Related Io functions
Tabular
arrayDatastore · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · mkdir · movefile · open · opentoline · path · pathsep · pcode · pwd · readstruct · rehash · restoredefaultpath · rmdir · rmpath · run · savepath · setenv · setpref · system · tempdir · tempname · uigetdir · uigetfile · uiputfile · unsetenv · userpath · what · winqueryreg · xmlread · xmlwrite
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how csvread is executed, line by line, in Rust.
- View the source for csvread in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.