Main Content

dlmwrite

(Not recommended) Write matrix to ASCII-delimited file

dlmwrite is not recommended. Use writematrix instead. For more information, see Compatibility Considerations.

Description

dlmwrite(filename,M) writes numeric data in array M to an ASCII format file, filename, using the default delimiter (,) to separate array elements. If the file, filename, already exists, dlmwrite overwrites the file.

example

dlmwrite(filename,M,'-append') appends the data to the end of the existing file, filename.

example

dlmwrite(___,Name,Value) additionally specifies delimiter, newline character, offset, and precision options using one or more name-value pair arguments.

example

dlmwrite(filename,M,delimiter) writes array M to the file, filename, using the specified delimiter, delimiter, to separate array elements.

dlmwrite(filename,M,delimiter,row,col) writes the array starting at the specified row and column row and col, in the destination file. Empty elements separated by delimiter fill the leading rows and columns.

Examples

collapse all

Create an array of sample data, M.

M = magic(3);

Write matrix M to a file myFile.txt.

dlmwrite('myFile.txt',M)

View the data in the file.

type('myFile.txt')
8,1,6
3,5,7
4,9,2

Create an array of sample data, M.

M = magic(3)*pi
M = 3×3

   25.1327    3.1416   18.8496
    9.4248   15.7080   21.9911
   12.5664   28.2743    6.2832

Write matrix M to a file, 'myFile.txt', delimited by the tab character and using a precision of 3 significant digits.

dlmwrite('myFile.txt',M,'delimiter','\t','precision',3)

View the data in the file.

type('myFile.txt')
25.1	3.14	18.8
9.42	15.7	22
12.6	28.3	6.28

Create two arrays of sample numeric data.

M = magic(5);
N = magic(3);

Export matrix M to a file and use whitespace as the delimiter.

dlmwrite('myFile.txt',M,'delimiter',' ');

Append matrix N to the file, offset from the existing data by one row. Then, view the file.

dlmwrite('myFile.txt',N,'-append',...
'delimiter',' ','roffset',1)
type('myFile.txt')
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
  
8 1 6
3 5 7
4 9 2

Read the data in 'myFile.txt' using dlmread.

dlmread('myFile.txt')
ans = 8×5

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
     8     1     6     0     0
     3     5     7     0     0
     4     9     2     0     0

When dlmread reads the two matrices from the file, it pads the smaller matrix with zeros.

Create an array of sample numeric data.

M = magic(3);

Export matrix M to a file using a precision of 6 decimal places.

dlmwrite('myFile.txt',M,'precision','%.6f');

View the data in the file.

type('myFile.txt')
8.000000,1.000000,6.000000
3.000000,5.000000,7.000000
4.000000,9.000000,2.000000

Input Arguments

collapse all

Name of file to write, specified as a character vector or string.

Example: 'myFile.txt' or "myFile.txt"

Data Types: char | string

Numeric data to write, specified as a matrix or a cell array of numeric values with one value per cell.

Example: [1,2,3;4,5,6]

Example: {1,2,3;4,5,6}

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | cell
Complex Number Support: Yes

Delimiter to separate array elements, specified as a character vector or string, containing a single character or characters of a control sequence. Use '\t' to produce tab-delimited files.

Example: ';' or ";"

Example: '\t' or "\t"

Data Types: char | string

Row offset, specified as a scalar. The row offset indicates the number of rows to skip before writing the numeric data. row is zero-based, so that row = 0 instructs MATLAB® to begin writing in the first row of the destination file. Skipped rows are populated with the specified delimiter.

Column offset, specified as a scalar. The column offset indicates the number of columns to skip before writing the numeric data. col is zero-based, so that col = 0 instructs MATLAB to begin writing in the first column of the destination file. Skipped columns are separated with the specified delimiter.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: dlmwrite('myFile.txt',M,'precision',4,'delimiter',' ') writes the numeric values in array M with four significant digits and delimited using the whitespace character.

Delimiter to separate array elements, specified as the comma-separated pair consisting of 'delimiter' and a character vector or string, containing a single character or characters of a control sequence. Use '\t' to produce tab-delimited files.

Example: 'delimiter',';' or 'delimiter',";"

Example: 'delimiter','\t' or 'delimiter',"\t"

Data Types: char | string

Row offset, specified as the comma-separated pair consisting of 'roffset' and a scalar. The row offset indicates the number of rows to skip before writing the numeric data. These rows are populated with the specified delimiter. When appending to an existing file, the new data is offset from the end of the existing data.

The row offset is zero-based, so that 'roffset',0 instructs MATLAB to begin writing in the first row of the destination file, which is the default. However, when appending to a file, 'roffset',0 instructs MATLAB to begin writing in the first row immediately following existing data.

Example: 'roffset',2

Column offset from the left side of the destination file, specified as the comma-separated pair consisting of 'coffset' and a scalar. The column offset indicates the number of columns to skip before writing the numeric data. These columns are separated with the specified delimiter.

The column offset is zero-based, so that 'coffset',0 instructs MATLAB to begin writing in the first column of the destination file, which is the default.

Example: 'coffset',1

Numeric precision to use in writing data to the file, specified as the comma-separated pair consisting of 'precision' and a scalar or a C-style format specifier that begins with %, such as '%10.5f'. If the value of precision is a scalar, then it indicates the number of significant digits.

Example: 'precision',3

Example: 'precision','%10.5f'

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char

Line terminator, specified as the comma-separated pair consisting of 'newline' and either 'pc' to use a carriage return/line feed (CR/LF), or 'unix' to use a line feed (LF).

Example: 'newline','pc'

Tips

  • dlmwrite writes a file that spreadsheet programs can read. Alternatively, if your system has Excel® for Windows® installed, you can create a spreadsheet using xlswrite.

Version History

Introduced before R2006a

collapse all

R2019a: dlmwrite is not recommended

dlmwrite is not recommended. Use writematrix instead. There are no plans to remove dlmwrite.

Starting in R2019a, use the writematrix function to write a matrix to a delimited text file. The writematrix function has better cross-platform support and performance over the dlmwrite function.

This table shows typical usages of dlmwrite and how to update your code to use writematrix instead.

Not Recommended

Recommended

dlmwrite('mydata.txt',M)
writematrix(M,'mydata.txt')