| MATLAB® | ![]() |
| On this page… |
|---|
Exporting Delimited ASCII Data Files |
This section describes how to use MATLAB functions to export data in several common ASCII formats. For example, you can use these functions to export a MATLAB matrix as a text file where the rows and columns are represented as space-separated, numeric values. The function you use depends on the amount of data you want to export and its format.
If you are not sure which section describes your data, refer to the table that follows and find the sample in it that most nearly matches the data format you want to create. Then read the section referred to in the table.
If you are familiar with MATLAB export functions, but are not sure when to use them, see ASCII Data Export Function Features, which compares the features of each function.
Note If C or Fortran routines for writing data files in the form needed by other applications exist, create a MEX-file to write the data. For more information, see the MATLAB External Interfaces documentation. |
Table 7-3. ASCII Data File Formats
Data Format Sample | MATLAB Export Function |
|---|---|
1 2 3 4 5 6 7 8 9 10 | See Exporting Delimited ASCII Data Files and Using the diary Function to Export Data for information about these options. |
1; 2; 3; 4; 5; 6; 7; 8; 9; 10; | See Exporting Delimited ASCII Data Files. The example shows a semicolon-delimited file, but you can specify another character as the delimiter. |
Table 7-4. ASCII Data Export Function Features
Function | Use With | Delimiters | Notes |
|---|---|---|---|
Numeric data | Commas only | Primarily used with spreadsheet data. See Working with Spreadsheets. | |
Numeric data or cell array | Spaces only | Use for small arrays. Requires editing of data file to remove extraneous text. | |
Numeric data | Any character | Easy to use, flexible. | |
Alphabetic and numeric data | Any character | Part of low-level file I/O routines. This function is the most flexible, but also the most difficult to use. You must use fopen to obtain a file identifier before writing the data and fclose to close the file after writing the data. | |
Numeric data | Tabs or spaces | Easy to use; output values are high precision. |
To export an array as a delimited ASCII data file, you can use either the save function, specifying the -ASCII qualifier, or the dlmwrite function. The save function is easy to use; however, the dlmwrite function provides more flexibility, allowing you to specify any character as a delimiter and to export subsets of an array by specifying a range of values.
To export the array A,
A = [ 1 2 3 4 ; 5 6 7 8 ];
use the save function, as follows:
save my_data.out A -ASCII
If you view the created file in a text editor, it looks like this:
1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000 5.0000000e+000 6.0000000e+000 7.0000000e+000 8.0000000e+000
By default, save uses spaces as delimiters, but you can use tabs instead of spaces by specifying the -tabs option.
When you use save to write a character array to an ASCII file, it writes the ASCII equivalent of the characters to the file. If you write the character string 'hello' to a file, save writes the values
104 101 108 108 111
To export an array in ASCII format and specify the delimiter used in the file, use the dlmwrite function.
For example, to export the array A,
A = [ 1 2 3 4 ; 5 6 7 8 ];
as an ASCII data file that uses semicolons as a delimiter, use this command:
dlmwrite('my_data.out',A, ';')
If you view the created file in a text editor, it looks like this:
1;2;3;4 5;6;7;8
Note that dlmwrite does not insert delimiters at the end of rows.
By default, if you do not specify a delimiter, dlmwrite uses a comma as a delimiter. You can specify a space (' ') as a delimiter or, if you specify empty quotes (''), no delimiter.
To export small numeric arrays or cell arrays, you can use the diary function. diary creates a verbatim copy of your MATLAB session in a disk file (excluding graphics).
For example, if you have the array A in your workspace,
A = [ 1 2 3 4; 5 6 7 8 ];
execute these commands at the MATLAB prompt to export this array using diary:
Turn on the diary function. You can optionally name the output file diary creates.
diary my_data.out
Display the contents of the array you want to export. This example displays the array A. You could also display a cell array or other MATLAB class.
A =
1 2 3 4
5 6 7 8
diary off
diary creates the file my_data.out and records all the commands executed in the MATLAB session until it is turned off.
A =
1 2 3 4
5 6 7 8
diary off
Open the diary file my_data.out in a text editor and remove all the extraneous text.
With the xmlwrite function, you can serialize a Document Object Model (DOM) node to an XML file.
MATLAB also provides these other XML functions:
xmlread — Imports from a given URL or file to a Document Object Model node
xslt — Transforms an XML document using an XSLT engine
For more information, see the reference pages for these functions.
![]() | Importing Text Data | Working with Graphics Files | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |