Quantcast

Documentation Center

  • Trial Software
  • Product Updates

dlmread

Read ASCII-delimited file of numeric data into matrix

Syntax

M = dlmread(filename)
M = dlmread(filename, delimiter)
M = dlmread(filename, delimiter, R, C)
M = dlmread(filename, delimiter, range)

Description

M = dlmread(filename) reads the ASCII-delimited numeric data file filename, and returns the data in output matrix M. The filename input is a string enclosed in single quotes. dlmread infers the delimiter from the formatting of the file.

M = dlmread(filename, delimiter) reads data from the file, using the specified delimiter. Use '\t' to specify a tab delimiter.

M = dlmread(filename, delimiter, R, C) reads data whose upper left corner is at row R and column C in the file. Values R and C are zero-based, so that R=0, C=0 specifies the first value in the file.

M = dlmread(filename, delimiter, range) reads the range specified by range = [R1 C1 R2 C2] where (R1,C1) is the upper left corner of the data to read and (R2,C2) is the lower right corner. You can also specify the range using spreadsheet notation, such as range = 'A1..B7'.

Examples

Example 1

Export a 5-by-8 test matrix M to a file, and read it with dlmread, first with no arguments other than the filename:

M = gallery('integerdata', 100, [5 8], 0); 
dlmwrite('myfile.txt', M, 'delimiter', '\t')

dlmread('myfile.txt')
ans =
    96    77    62    41     6    21     2    42
    24    46    80    94    36    20    75    85
    61     2    93    92    82    61    45    53
    49    83    74    42     1    28    94    21
    90    45    18    90    14    20    47    68

Now read a portion of the matrix by specifying the row and column of the upper left corner:

dlmread('myfile.txt', '\t', 2, 3)
ans =
    92    82    61    45    53
    42     1    28    94    21
    90    14    20    47    68

This time, read a different part of the matrix using a range specifier:

dlmread('myfile.txt', '\t', 'C1..G4')
ans =
    62    41     6    21     2
    80    94    36    20    75
    93    92    82    61    45
    74    42     1    28    94

Example 2

Export matrix M to a file, and then append an additional matrix to the file that is offset one row below the first:

M = magic(3);
dlmwrite('myfile.txt', [M*5 M/5], ' ')

dlmwrite('myfile.txt', [M/3], '-append', ...
   'roffset', 1, 'delimiter', ' ')

type myfile.txt

40 5 30 1.6 0.2 1.2
15 25 35 0.6 1 1.4
20 45 10 0.8 1.8 0.4
  
2.6667 0.33333 2
1 1.6667 2.3333
1.3333 3 0.66667

When dlmread imports these two matrices from the file, it pads the smaller matrix with zeros:

dlmread('myfile.txt')
   40.0000    5.0000   30.0000    1.6000    0.2000    1.2000
   15.0000   25.0000   35.0000    0.6000    1.0000    1.4000
   20.0000   45.0000   10.0000    0.8000    1.8000    0.4000
    2.6667    0.3333    2.0000         0         0         0
    1.0000    1.6667    2.3333         0         0         0
    1.3333    3.0000    0.6667         0         0         0

More About

expand all

Tips

  • All data in the input file must be numeric. dlmread does not read files that contain nonnumeric data, even if the specified rows and columns contain only numeric data.

  • When dlmread infers the delimiter from the formatting of the file, it treats repeated white spaces as a single delimiter. By contrast, if you specify a delimiter, dlmread treats any repeated delimiter character as a separate delimiter.

  • If you want to specify an R, C, or range input, but not a delimiter, set the delimiter argument to the empty string, (two consecutive single quotes with no spaces in between, ''). For example,

    M = dlmread('myfile.dat', '', 5, 2)

    In this case, dlmread treats repeated white spaces as a single delimiter.

  • dlmread fills empty delimited fields with zero. If each line ends with a nonspace delimiter, such as a semicolon, the output matrix contains an additional last column of zeros.

  • dlmread imports any complex number as a whole into a complex numeric field. Valid forms for a complex number are

    ±<real>±<imag>i|j

    Example: 5.7-3.1i

    ±<imag>i|j

    Example: -7j

    Embedded white-space in a complex number is invalid and is regarded as a field delimiter.

See Also

| |

Was this topic helpful?