Documentation

dlmwrite

Write matrix to ASCII-delimited file

Syntax

dlmwrite(filename,M)
dlmwrite(filename,M,'-append')
dlmwrite(___,Name,Value)
dlmwrite(filename,M,delimiter)
dlmwrite(filename,M,delimiter,row,col)

Description

example

dlmwrite(filename,M)writes numeric data in arrayMto an ASCII format file,filename, using the default delimiter (,) to separate array elements. If the file,filename, already exists,dlmwriteoverwrites 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 arrayMto 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 columnrowandcol, in the destination file. Empty elements separated bydelimiterfill the leading rows and columns.

Examples

collapse all

Create an array of sample data,M.

M = magic(3);

Write matrixMto 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

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 matrixMto 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 matrixMto a file and use whitespace as the delimiter.

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

Append matrixNto 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'usingdlmread.

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

Whendlmreadreads the two matrices from the file, it pads the smaller matrix with zeros.

Create an array of sample numeric data.

M = magic(3);

Export matrixMto 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.rowis zero-based, so thatrow = 0instructs 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.colis zero-based, so thatcol = 0指示MATLAB开始写作前坳umn of the destination file. Skipped columns are separated with the specified delimiter.

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside single quotes (' '). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN.

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

collapse all

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”,0instructs MATLAB to begin writing in the first row of the destination file, which is the default. However, when appending to a file,“roffset”,0instructs 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指示MATLAB开始写作前坳umn 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 ofprecisionis 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

  • dlmwritewrites a file that spreadsheet programs can read. Alternatively, if your system has Excel®for Windows®installed, you can create a spreadsheet usingxlswrite.

Introduced before R2006a

Was this topic helpful?