csvread — Read numeric data from comma-separated text files.
csvread(filename) imports real or complex numeric CSV text as a dense host double matrix. Row and column offsets are zero-based; readmatrix is the broader import interface.
Syntax
M = csvread(filename)
M = csvread(filename, row, col)
M = csvread(filename, row, col, range)Inputs
filename- Type
StringScalar- Required
- Yes
- Description
- CSV file path.
row- Type
IntegerScalar- Required
- Yes
- Description
- Zero-based starting row offset.
col- Type
IntegerScalar- Required
- Yes
- Description
- Zero-based starting column offset.
range- Type
Any- Required
- Yes
- Description
- A1-style range string or numeric range vector.
Returns
M- Type
NumericArray- Description
- Numeric matrix read from the CSV file.
Errors
- When
- Argument list does not match supported csvread call forms.
- Message
csvread: invalid argument configuration
- When
- Row/column offset arguments are invalid.
- Message
csvread: invalid row/column index
- When
- Range argument is malformed or semantically invalid.
- Message
csvread: invalid range
Forms and ranges
The filename is a character row or string scalar, including a one-element string array. csvread(filename,row,col) skips zero-based rows and columns; the range form takes [r1 c1 r2 c2] or an inclusive A1 range such as "B2..D6". Colon-separated A1 ranges and two-element numeric ranges require their separate RunMat extensions.
Empty fields, including a trailing comma, become zero. Case-insensitive NaN, Inf, and -Inf are accepted. Numeric fields may be quoted. A nonnumeric token raises an error with its row and column. Input bytes in skipped headers or label columns need not be valid UTF-8.
An empty file returns a 0-by-0 double tensor. A complex token (a+bi, a+bj, or a pure imaginary value) produces complex-double storage even when its imaginary component is zero; ordinary real data remains double. General quoted-field CSV escaping is outside this narrow reader; use readmatrix or readtable for mixed data.
Filesystem and residency
Relative paths use the captured working directory; ~ expands to the home directory before opening, without changing that directory. The selected filesystem provider and any advertised resource lease remain attached across suspension and parsing. A provider without a usable lease cannot silently fall back to ambient I/O.
Parsing and output construction occur on the host and form a fusion boundary. Public host controls need no acceleration provider. Resident filename, offset, or range controls require the csvread-resident-control-inputs extension and are gathered through their owning provider; the result remains on the host. Apply gpuArray(csvread(...)) if a device copy is wanted.
A worker requires its own accessible filesystem source; the filename is not a transferable snapshot. CPU work cooperates with the installed execution service, but finite complete-call memory evidence for file bytes, parsed rows, and output remains a shared resource-admission requirement.
Historical home-directory illustration (retained but not run by the isolated example harness):
`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);`
Displayed output: `M =
9 10
11 12`. The executable gallery uses isolated workspace fixtures rather than creating files in the user's real home directory.
Examples
Import an entire CSV file
M = csvread('scores.csv');
MExpected output:
M =
1 2 3
4 5 6Skip a header row and label column
M = csvread('with_header.csv',1,1);
MExpected output:
M =
1 2
3 4Select a numeric rectangle
block = csvread('measurements.csv',0,0,[1 1 2 3])Expected output:
block =
15 16 17
19 20 21Use an Excel-style range
sub = csvread('measurements2.csv',0,0,'B2:C3')Expected output:
sub =
15 16
19 20Interpret empty fields as zero
M = csvread('with_blanks.csv')Expected output:
M =
1 0 3
0 5 0
7 8 0Report a nonnumeric field
try
csvread('bad.csv');
catch err
disp(err.message);
endExpected 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?⌄
It accepts numeric CSV fields, not arbitrary mixed text. Use readmatrix or readtable for broader imports.
Are row and column offsets zero-based?⌄
Yes; they count rows and columns skipped from the beginning.
How are Excel-style ranges interpreted?⌄
The public double-dot A1 form includes both endpoints. Colon notation is separately gated as a RunMat extension.
Can quoted numeric fields be read?⌄
Yes; general quoted CSV escaping is not part of this narrow numeric reader.
What happens to empty fields?⌄
They become zero, including trailing fields.
Does csvread support custom delimiters?⌄
No; it uses commas. Use dlmread or readmatrix for other delimiters.
How can a result be moved to a GPU?⌄
The reader returns a host tensor. Call gpuArray on it afterward.
What if the file is empty?⌄
The result is a 0-by-0 double tensor.
Does csvread change the working directory?⌄
No; relative paths use its captured value without modifying it.
Related Io functions
Tabular
arrayDatastore · csvwrite · detectImportOptions · dlmread · dlmwrite · fileDatastore · getvaropts · parquetDatastore · parquetinfo · parquetread · readcell · readmatrix · readtable · readtimetable · setvaropts · setvartype · spreadsheetImportOptions · writecell · writematrix · writetable · writetimetable · xlsread · xlswrite
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fileattrib · fileparts · filesep · fullfile · genpath · getenv · getpref · isenv · isfile · isfolder · ispref · ls · matlabroot · memmapfile · memmapfile.subsasgn · memmapfile.subsref · 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
Data
commit · data.copy · data.create · data.delete · data.exists · data.export · data.import · data.inspect · data.list · data.move · data.open · DataArray.chunk_shape · DataArray.codec · DataArray.dtype · DataArray.fill · DataArray.name · DataArray.rank · DataArray.read · DataArray.resize · DataArray.shape · DataArray.write · Dataset.array · Dataset.arrays · Dataset.attrs · Dataset.begin · Dataset.get_attr · Dataset.has_array · Dataset.id · Dataset.path · Dataset.refresh · Dataset.set_attr · Dataset.set_attrs · Dataset.snapshot · Dataset.version · DataTransaction.abort · DataTransaction.commit · DataTransaction.create_array · DataTransaction.delete_array · DataTransaction.fill · DataTransaction.id · DataTransaction.resize · DataTransaction.set_attr · DataTransaction.set_attrs · DataTransaction.status · DataTransaction.write
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite · readlines · writelines
Import
Json
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.