Documentation

dlmwrite

Write matrix to ASCII-delimited file

Syntax

  • dlmwrite(filename,M,delimiter)
  • dlmwrite(filename,M,delimiter,row,col)

Description

example

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.

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

expand all

Write Comma-Separated Data

Create an array of sample data, M.

M = magic(3);

Write matrix M to a file, 'myFile.txt', using the default delimiter (,).

dlmwrite('myFile.txt',M)

View the data in the file.

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

Write Tab-Delimited Data and Specify Precision

Create an array of sample data, M.

M = magic(3)*pi
M =

   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

Write and Append Data to File

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 =

    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.

Write Data and Specify Precision As String

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

expand all

filename — Name of file to writestring

Name of file to write, specified as a string.

Example: 'myFile.txt'

Data Types: char

M — Numeric data to writematrix | cell array of numeric values

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 — Delimiter to separate array elements',' (default) | string

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

Example: ';'

Example: '\t'

Data Types: char

row — Row offset0 (default) | scalar

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.

col — Column offset0 (default) | scalar

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 Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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' — Delimiter to separate array elements',' (default) | string

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

Example: 'delimiter',';'

Example: 'delimiter','\t'

Data Types: char

'roffset' — Row offset0 (default) | scalar

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

'coffset' — Column offset0 (default) | scalar

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

'precision' — Numeric precision5 (default) | scalar | C-style format string

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 string 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

'newline' — Line terminator'pc' | 'unix'

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'

More About

expand all

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.

Was this topic helpful?