Write to Delimited Data Files

Export Numeric Array to ASCII File

To export a numeric array as a delimited ASCII data file, you can use either the save function, specifying the -ASCII qualifier, or the dlmwrite function.

Both save and dlmwrite are easy to use. With dlmwrite, you can specify any character as a delimiter, and you can export subsets of an array by specifying a range of values.

However, save -ascii and dlmwrite do not accept cell arrays as input. To create a delimited ASCII file from the contents of a cell array, you can first convert the cell array to a matrix using the cell2mat function, and then call save or dlmwrite. Use this approach when your cell array contains only numeric data, and easily translates to a two-dimensional numeric array.

Export Numeric Array to ASCII File Using save

To export the array A, where

A = [ 1 2 3 4 ; 5 6 7 8 ];

to a space-delimited ASCII data file, use the save function as follows:

save my_data.out A -ASCII

To view the file, use the type function:

type my_data.out

1.0000000e+000  2.0000000e+000  3.0000000e+000  4.0000000e+000
5.0000000e+000  6.0000000e+000  7.0000000e+000  8.0000000e+000

When you use save to write a character array to an ASCII file, it writes the ASCII equivalent of the characters to the file. For example, if you write the character string 'hello' to a file, save writes the values

104 101 108 108 111

to the file in 8-digit ASCII format.

To write data in 16-digit format, use the -double option. To create a tab-delimited file instead of a space-delimited file, use the -tabs option.

Export Numeric Array to ASCII File Using dlmwrite

To export a numeric or character array to an ASCII file with a specified delimiter, use the dlmwrite function.

For example, to export the array A,

A = [ 1 2 3 4 ; 5 6 7 8 ];

to an ASCII data file that uses semicolons as a delimiter, use this command:

dlmwrite('my_data.out',A, ';')

To view the file, use the type function:

type my_data.out

1;2;3;4
5;6;7;8

By default, dlmwrite uses a comma as a delimiter. You can specify a space (' ') or other character as a delimiter. To specify no delimiter, use empty quotation marks ('').

Export Table to Text File

This example shows how to export a table to a text file, using the writetable function.

Create a sample table, T, for exporting.

Name = {'M4';'M5';'M6';'M8';'M10'};
Pitch = [0.7;0.8;1;1.25;1.5];
Shape = {'Pan';'Round';'Button';'Pan';'Round'};
Price = [10.0;13.59;10.50;12.00;16.69];
Stock = [376;502;465;1091;562];
T = table(Pitch,Shape,Price,Stock,'RowNames',Name)
T = 

           Pitch     Shape      Price    Stock
           _____    ________    _____    _____

    M4      0.7     'Pan'          10     376 
    M5      0.8     'Round'     13.59     502 
    M6        1     'Button'     10.5     465 
    M8     1.25     'Pan'          12    1091 
    M10     1.5     'Round'     16.69     562 

The table has both column headings and row names.

Export the table, T, to a text file named tabledata.txt.

writetable(T,'tabledata.txt')

View the file.

type tabledata.txt
Pitch,Shape,Price,Stock
0.7,Pan,10,376
0.8,Round,13.59,502
1,Button,10.5,465
1.25,Pan,12,1091
1.5,Round,16.69,562

By default, writetable writes comma-separated data, includes table variable names as column headings, and does not write row names.

Export table T to a tab-delimited text file named tabledata2.txt and write the row names in the first column of the output. Use the Delimiter name-value pair argument to specify a tab delimiter, and the WriteRowNames name-value pair argument to include row names.

writetable(T,'tabledata2.txt','Delimiter','\t','WriteRowNames',true)

View the file.

type tabledata2.txt
Row	Pitch	Shape	Price	Stock
M4	0.7	Pan	10	376
M5	0.8	Round	13.59	502
M6	1	Button	10.5	465
M8	1.25	Pan	12	1091
M10	1.5	Round	16.69	562

Export Cell Array to Text File

Export Cell Array Using fprintf

This example shows how to export a cell array to a text file, using the fprintf function.

Create a sample cell array, C, for exporting.

C = {'Atkins',32,77.3,'M';'Cheng',30,99.8,'F';'Lam',31,80.2,'M'}
C = 

    'Atkins'    [32]    [77.3000]    'M'
    'Cheng'     [30]    [99.8000]    'F'
    'Lam'       [31]    [80.2000]    'M'

Open a file named celldata.dat for writing.

fileID = fopen('celldata.dat','w');

fopen returns a file identifier, fileID, that fprintf calls to write to the file.

Describe the pattern of the file data using format specifiers. Typical format specifiers include '%s' for a string, '%d' for an integer, or '%f' for a floating-point number. Separate each format specifier with a space to indicate a space delimiter for the output file. Include a newline character at the end of each row of data ('\n').

formatSpec = '%s %d %2.1f %s\n';

Some Windows® text editors, including Microsoft® Notepad, require a newline character sequence of '\r\n' instead of '\n'. However, '\n' is sufficient for Microsoft Word or WordPad.

Determine the size of C. Then, export one row of data at a time using the fprintf function.

[nrows,ncols] = size(C);
for row = 1:nrows
    fprintf(fileID,formatSpec,C{row,:});
end

fprintf writes a space-delimited file.

Close the file.

fclose(fileID);

View the file.

type celldata.dat
Atkins 32 77.3 M
Cheng 30 99.8 F
Lam 31 80.2 M

Convert Cell Array to Table for Export

This example shows how to convert a cell array of mixed text and numeric data to a table before writing the data to a text file. Tables are suitable for column-oriented or tabular data. You then can write the table to a text file using the writetable function.

Convert the cell array, C, from the previous example, to a table using the cell2table function. Add variable names to each column of data using the VariableNames name-value pair argument.

T = cell2table(C,'VariableNames',{'Name','Age','Result','Gender'});

Write table T to a text file.

writetable(T,'tabledata.dat')

View the file.

type tabledata.dat
Name,Age,Result,Gender
Atkins,32,77.3,M
Cheng,30,99.8,F
Lam,31,80.2,M

See Also

| | | |

Was this topic helpful?