Exporting Text Data

Overview

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.

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

csvwrite

Numeric data

Commas only

Primarily used with spreadsheet data. See Working with Spreadsheets.

diary

Numeric data or cell array

Spaces only

Use for small arrays. Requires editing of data file to remove extraneous text.

dlmwrite

Numeric data

Any character

Easy to use, flexible.

fprintf

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.

save

Numeric data

Tabs or spaces

Easy to use; output values are high precision.

Exporting Delimited ASCII Data Files

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.

Using the save Function

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

Using the dlmwrite Function

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.

Using the diary Function to Export Data

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:

  1. Turn on the diary function. You can optionally name the output file diary creates.

    diary my_data.out
    
  2. 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
    
  3. Turn off the diary function.

    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
    
  4. Open the diary file my_data.out in a text editor and remove all the extraneous text.

Exporting to XML Documents

With the xmlwrite function, you can serialize a Document Object Model (DOM) node to an XML file.

MATLAB also provides these other XML functions:

For more information, see the reference pages for these functions.

  


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