MATLAB Function Reference |
Write matrix to ASCII-delimited file
Syntax
dlmwrite('filename', M) dlmwrite('filename', M, 'D') dlmwrite('filename', M, 'D', R, C) dlmwrite('filename', M, attribute1, value1, attribute2, value2, ...) dlmwrite('filename', M, '-append') dlmwrite('filename', M, '-append', attribute-value list)
Description
dlmwrite('filename', M)
writes matrix M
into an ASCII format file using the default delimiter (,
) to separate matrix elements. The data is written starting at the first column of the first row in the destination file, filename
.
dlmwrite('filename', M, 'D')
writes matrix M
into an ASCII format file, using delimiter D
to separate matrix elements. The data is written starting at the first column of the first row in the destination file, filename
. A comma (,
) is the default delimiter. Use \t
to produce tab-delimited files.
dlmwrite('filename', M, 'D', R, C)
writes matrix A
into an ASCII format file, using delimiter D
to separate matrix elements. The data is written starting at row R
and column C
in the destination file, filename
. R
and C
are zero based, so that R=0
, C=0
specifies the first value in the file, which is the upper left corner.
dlmwrite('filename', M, 'attrib1', value1, 'attrib2', value2, ...)
is an alternate syntax to those shown above, in which you specify any number of attribute-value pairs in any order in the argument list. Each attribute must be immediately followed by a corresponding value (see the table below).
This table shows which values you can use when setting the newline
attribute.
Line Terminator |
Description |
'pc' |
PC terminator (implies carriage return/line feed (CR/LF )) |
'unix' |
UNIX terminator (implies line feed (LF )) |
dlmwrite('filename', M, '-append')
appends the matrix to the file. If you do not specify '-append'
, dlmwrite
overwrites any existing data in the file.
dlmwrite('filename', M, '-append', attribute-value list)
is the same as the syntax shown above, but accepts a list of attribute-value
pairs. You can place the '-append'
flag in the argument list anywhere between attribute-value
pairs, but not in between an attribute
and its value
.
Remarks
The resulting file is readable by spreadsheet programs.
Examples
Export matrix M
to a file delimited by the tab character and using a precision of six significant digits:
dlmwrite('myfile.txt', M, 'delimiter', '\t', 'precision', 6) type myfile.txt 0.893898 0.284409 0.582792 0.432907 0.199138 0.469224 0.423496 0.22595 0.298723 0.0647811 0.515512 0.579807 0.661443 0.988335 0.333951 0.760365
Export matrix M
to a file using a precision of six decimal places and the conventional line terminator for the PC platform:
dlmwrite('myfile.txt', m, 'precision', '%.6f', 'newline', 'pc') type myfile.txt 16.000000,2.000000,3.000000,13.000000 5.000000,11.000000,10.000000,8.000000 9.000000,7.000000,6.000000,12.000000 4.000000,14.000000,15.000000,1.000000
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(4); dlmwrite('myfile.txt', [M*5 M/5], ' ') dlmwrite('myfile.txt', rand(3), 'append', 'on', ... 'roffset', 1, 'delimiter', ' ') type myfile.txt 80 10 15 65 3.2 0.4 0.6 2.6 25 55 50 40 1 2.2 2 1.6 45 35 30 60 1.8 1.4 1.2 2.4 20 70 75 5 0.8 2.8 3 0.2 0.99008 0.49831 0.32004 0.78886 0.21396 0.9601 0.43866 0.64349 0.72663
See Also
dlmread
, csvwrite
, csvread
, wk1write
, wk1read
dlmread | dmperm |
© 1994-2005 The MathWorks, Inc.