dlmread — Read numeric data from delimiter-separated text files in MATLAB and RunMat.
dlmread(filename) reads numeric data from delimiter-separated ASCII text files. Custom delimiter, range, and offset forms follow MATLAB and RunMat compatibility semantics.
Syntax
M = dlmread(filename)
M = dlmread(filename, delimiter)
M = dlmread(filename, row, col)
M = dlmread(filename, delimiter, range)
M = dlmread(filename, delimiter, row, col)
M = dlmread(filename, delimiter, row, col, range)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
filename | StringScalar | Yes | — | Delimited text file path. |
delimiter | Any | Yes | — | Delimiter as string/char or numeric code. |
row | IntegerScalar | Yes | — | Zero-based starting row offset. |
col | IntegerScalar | Yes | — | Zero-based starting column offset. |
range | Any | Yes | — | Range as A1-style string or numeric range vector. |
Returns
| Name | Type | Description |
|---|---|---|
M | NumericArray | Numeric matrix parsed from the delimited text file. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | Argument list does not match supported dlmread call forms. | dlmread: invalid argument configuration |
| — | Delimiter value is invalid. | dlmread: invalid delimiter |
| — | Row/column indices are invalid. | dlmread: invalid row/column index |
| — | Range specification is malformed or semantically invalid. | dlmread: invalid range |
| — | Filename argument is not a scalar string/char vector. | dlmread: invalid filename input |
| — | Filename resolves to an empty path. | dlmread: filename must not be empty |
| — | Input file cannot be opened. | dlmread: unable to open file |
| — | Input file cannot be read. | dlmread: failed to read file |
| — | A token cannot be parsed as numeric in the selected import region. | dlmread: nonnumeric token encountered |
| — | Internal tensor output materialization fails. | dlmread: unable to construct output matrix |
How dlmread works
- Accepts character vectors or string scalars for
filename. String arrays must contain exactly one element. - The delimiter is optional; when omitted, a comma (
,) is used. Supply a character (for example';'), a string scalar ("\t"), or a numeric ASCII code (9for tab). Empty delimiter inputs are not allowed. dlmread(filename, R, C)anddlmread(filename, delimiter, R, C)start reading at zero-based offsetsRandC, skipping any earlier rows or columns.dlmread(filename, range)anddlmread(filename, delimiter, range)accept a numeric vector[r1 c1 r2 c2]or[r1 c1]describing a rectangular slice. The indices are zero-based and inclusive. Excel-style addresses such as"B2:D6"are also accepted for compatibility withcsvread.- Empty fields (for example two adjacent delimiters) 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 using one-based indices (matching MATLAB diagnostic messages).
- Results are dense double-precision tensors laid out in column-major order. Empty files yield a
0×0tensor. - Leading UTF-8 byte order marks (BOM) are stripped automatically to match MATLAB's handling of spreadsheets that emit BOM-prefixed text files.
- Paths may include
~to reference the home directory; RunMat expands the token before opening the file.
Does RunMat run dlmread on the GPU?
dlmread performs file I/O and parsing on the CPU. Arguments are gathered from the GPU when needed, and the output tensor lives in host memory. Wrap the result in gpuArray if you need residency on a device. No provider hooks are involved.
GPU memory and residency
dlmread always creates a CPU-resident tensor because the function performs file I/O synchronously on the host. If you need the data on the GPU, call gpuArray(dlmread(...)) or switch to readmatrix with the 'Like' option to direct the result to a device.
Examples
Reading comma-delimited data by default
writematrix([1 2 3; 4 5 6], "samples.csv");
M = dlmread("samples.csv")
delete("samples.csv");Expected output:
M =
1 2 3
4 5 6Importing semicolon-separated values
fid = fopen("scores.txt", "w");
fprintf(fid, "1;2;3\n4;5;6\n");
fclose(fid);
M = dlmread("scores.txt", ";")
delete("scores.txt");Expected output:
M =
1 2 3
4 5 6Using tab characters as the delimiter
fid = fopen("tabs.txt", "w");
fprintf(fid, "10\t11\t12\n13\t14\t15\n");
fclose(fid);
M = dlmread("tabs.txt", "\t")
delete("tabs.txt");Expected output:
M =
10 11 12
13 14 15Skipping a header row and column (zero-based offsets)
fid = fopen("with_header.txt", "w");
fprintf(fid, "Label,Jan,Feb\nalpha,1,2\nbeta,3,4\n");
fclose(fid);
M = dlmread("with_header.txt", ",", 1, 1)
delete("with_header.txt");Expected output:
M =
1 2
3 4Extracting a rectangular range
fid = fopen("block.txt", "w");
fprintf(fid, "10,11,12,13\n14,15,16,17\n18,19,20,21\n");
fclose(fid);
sub = dlmread("block.txt", ",", [1 1 2 3])
delete("block.txt");Expected output:
sub =
15 16 17
19 20 21Treating empty fields as zeros
fid = fopen("blanks.txt", "w");
fprintf(fid, "1,,3\n,5,\n7,8,\n");
fclose(fid);
M = dlmread("blanks.txt")
delete("blanks.txt");Expected output:
M =
1 0 3
0 5 0
7 8 0Using a numeric ASCII code for the delimiter
fid = fopen("pipe.txt", "w");
fprintf(fid, "5|6|7\n8|9|10\n");
fclose(fid);
M = dlmread("pipe.txt", double('|'))
delete("pipe.txt");Expected output:
M =
5 6 7
8 9 10Using dlmread with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how dlmread changes the result.
Run a small dlmread example, explain the result, then change one input and compare the output.
FAQ
Can I omit the delimiter argument?⌄
Yes. When you omit the delimiter, dlmread assumes a comma. If you need another delimiter, pass it explicitly as a character, string scalar, or numeric ASCII code.
Are row and column offsets zero-based like MATLAB?⌄
Yes. The R and C arguments count from zero. dlmread(filename, delimiter, 1, 2) skips the first row and the first two columns before reading data.
How do I specify a range?⌄
Provide a numeric vector [r1 c1 r2 c2] (zero-based, inclusive) or an Excel-style address such as "B2:D5". You can pass the range with or without a delimiter argument.
What happens if the file contains text tokens?⌄
Non-numeric fields trigger an error that includes the one-based row and column number of the offending token. Use readmatrix or readtable when you need to mix text and numbers.
How does dlmread treat empty cells?⌄
Empty cells evaluate to 0, matching MATLAB behavior. This includes consecutive delimiters and trailing delimiters.
Can I use whitespace as the delimiter?⌄
Yes. Pass " " (space), "\t" (tab), or the corresponding ASCII code. Multiple consecutive delimiters produce zeros where the values are missing.
Does dlmread respect locale-specific decimal separators?⌄
No. Parsing always uses . as the decimal separator, consistent with MATLAB.
Does dlmread change the working directory?⌄
No. Relative paths are resolved against the current working directory. dlmread never mutates global process state.
Why does the output stay on the CPU?⌄
dlmread performs synchronous file I/O, so the result resides in host memory. Wrap the result with gpuArray if you want a device-resident tensor.
Related Io functions
Tabular
csvread · csvwrite · detectImportOptions · dlmwrite · readmatrix · spreadsheetImportOptions · writecell · writematrix · xlsread
Repl Fs
addpath · cd · copyfile · delete · dir · exist · fullfile · genpath · getenv · ls · mkdir · movefile · path · pwd · rmdir · rmpath · run · savepath · setenv · tempdir · tempname · uigetfile · uiputfile
Filetext
fclose · feof · fgetl · fgets · fileread · filewrite · fopen · fprintf · fread · frewind · fwrite
Import
Json
Archive
Http
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dlmread is executed, line by line, in Rust.
- View the source for dlmread 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.