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
dlmwrite(___,
additionally specifies delimiter, newline character, offset, and precision options using one or more name-value pair arguments.Name,Value
)
Examples
写通讯a-Separated Data
Create an array of sample data,M
.
M = magic(3);
Write matrixM
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 matrixM
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 matrixM
to a file and use whitespace as the delimiter.
dlmwrite('myFile.txt',M,'delimiter',' ');
Append matrixN
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'
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
Whendlmread
reads the two matrices from the file, it pads the smaller matrix with zeros.
Write Data and Specify Precision
Create an array of sample numeric data.
M = magic(3);
Export matrixM
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
filename
—Name of file to write
character vector|string
Name of file to write, specified as a character vector or string.
Example:'myFile.txt'
or"myFile.txt"
Data Types:char
|string
M
—Numeric data to write
matrix|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) |character vector|string
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
—Row offset
0(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 thatrow = 0
instructs MATLAB®to begin writing in the first row of the destination file. Skipped rows are populated with the specified delimiter.
col
—Column offset
0(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 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,Value
arguments.Name
is the argument name andValue
is the corresponding value.Name
must appear inside single quotes (' '
). You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN
.
dlmwrite('myFile.txt',M,'precision',4,'delimiter',' ')
writes the numeric values in arrayM
with four significant digits and delimited using the whitespace character.
'delimiter'
—Delimiter to separate array elements
','
(default) |character vector|string
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
“roffset”
—Row offset
0(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 offset
0(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
指示MATLAB开始写作前坳umn of the destination file, which is the default.
Example:'coffset',1
'precision'
—Numeric precision
5(default) |scalar|C-style format specifier|character vector
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 ofprecision
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'
Tips
dlmwrite
writes a file that spreadsheet programs can read. Alternatively, if your system has Excel®for Windows®installed, you can create a spreadsheet usingxlswrite
.
See Also
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)