Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

dlmwrite - Write matrix to ASCII-delimited file

Syntax

dlmwrite(filename, M)
dlmwrite(filename, M, 'D')
dlmwrite(filename, M, 'D', R, C)
dlmwrite(filename, M, 'attrib1', value1, 'attrib2', 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. The filename input is a string enclosed in single quotes.

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 M 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).

Attribute

Value

delimiter

Delimiter string to be used in separating matrix elements

newline

Character(s) to use in terminating each line (see table below)

roffset

Offset, in rows, from the top of the destination file to where matrix data is to be written. Offset is zero based.

coffset

Offset, in columns, from the left side of the destination file to where matrix data is to be written. Offset is zero based.

precision

Numeric precision to use in writing data to the file. Specify the number of significant digits or a C-style format string starting in %, such as '%10.5f'.

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. Alternatively, if your system has Excel for Windows installed, you can create a spreadsheet using xlswrite.

The dlmwrite function does not accept cell arrays for the input matrix M. To export a cell array that contains only numeric data, use cell2mat to convert the cell array to a numeric matrix before calling dlmwrite. For other cases, use low-level export functions. For more information, see Exporting a Cell Array to a Text File in the MATLAB Data Import and Export documentation.

Examples

Example 1

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

Example 2

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

Example 3

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(3);
dlmwrite('myfile.txt', [M*5 M/5], ' ')

dlmwrite('myfile.txt', rand(3), '-append', ...
   'roffset', 1, 'delimiter', ' ')

type myfile.txt

40 5 30 1.6 0.2 1.2
15 25 35 0.6 1 1.4
20 45 10 0.8 1.8 0.4

0.81472 0.91338 0.2785
0.90579 0.63236 0.54688
0.12699 0.09754 0.95751

When dlmread imports these two matrices from the file, it pads the smaller matrix with zeros:

dlmread('myfile.txt')
   40.0000    5.0000   30.0000   1.6000   0.2000   1.2000
   15.0000   25.0000   35.0000   0.6000   1.0000   1.4000
   20.0000   45.0000   10.0000   0.8000   1.8000   0.4000
    0.8147    0.9134    0.2785   0        0        0
    0.9058    0.6324    0.5469   0        0        0
    0.1270    0.0975    0.9575   0        0        0

See Also

dlmread, csvwrite, csvread, fileformats

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS