Write Data to Text Files

Export tabular data contained in tables, cell arrays, or numeric arrays from the MATLAB® workspace to text files.

Export Table to Text File

You can export tabular data from MATLAB® workspace into a text file using the writetable function. Create a sample table, write the table to text file, and then write the table to text file with additional options.

Create a sample table, T, containing the variables Pitch, Shape, Price and Stock.

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)
T=5×4 table
    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 

Export the table, T, to a text file named tabledata.txt. View the contents of the file. By default, writetable writes comma-separated data, includes table variable names as column headings.

writetable(T,'tabledata.txt');
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

Create a table T2 which includes row names using the RowNames name-value pair argument.

rowNames = {'M4';'M5';'M6';'M8';'M10'};
T2 = table(Pitch,Shape,Price,Stock,'RowNames',rowNames)
T2=5×4 table
           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 T2 to a tab-delimited text file named tabledata2.txt. Use the Delimiter name-value pair argument to specify a tab delimiter, and the WriteRowNames name-value pair argument to include row names. View the contents of the file.

writetable(T2,'tabledata2.txt','Delimiter','\t','WriteRowNames',true);
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

You can export a cell array from MATLAB® workspace into a text file in one of these ways:

  • Use the writecell function to export the cell array to a text file.

  • Use fprintf to export the cell array by specifying the format of the output data.

Create a sample cell array C.

C = {'Atkins',32,77.3,'M';'Cheng',30,99.8,'F';'Lam',31,80.2,'M'}
C = 3×4 cell array
    {'Atkins'}    {[32]}    {[77.3000]}    {'M'}
    {'Cheng' }    {[30]}    {[99.8000]}    {'F'}
    {'Lam'   }    {[31]}    {[80.2000]}    {'M'}

Export the cell array using writecell.

writecell(C,'data.dat')

View the contents of the file.

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

Alternatively, import the cell array using fprintf. Open a file that you can write to named celldata.dat. Define formatSpec using the format specifiers to describe the pattern of the data in the file. Typical format specifiers include '%s' for a character vector, '%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').

fileID = fopen('celldata.dat','w');
formatSpec = '%s %d %2.1f %s\n';

Determine the size of C and export one row of data at a time using the fprintf function. Then close the file. fprintf writes a space-delimited file.

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

View the contents of the file.

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

Export Numeric Array to Text File

You can export a numerical array to a text file using writematrix.

Create a numeric array A.

A = magic(5)/10 
A = 5×5

    1.7000    2.4000    0.1000    0.8000    1.5000
    2.3000    0.5000    0.7000    1.4000    1.6000
    0.4000    0.6000    1.3000    2.0000    2.2000
    1.0000    1.2000    1.9000    2.1000    0.3000
    1.1000    1.8000    2.5000    0.2000    0.9000

Write the numeric array to myData.dat and specify the delimiter to be ';'. Then, view the contents of the file.

writematrix(A,'myData.dat','Delimiter',';')  
type myData.dat
1.7;2.4;0.1;0.8;1.5
2.3;0.5;0.7;1.4;1.6
0.4;0.6;1.3;2;2.2
1;1.2;1.9;2.1;0.3
1.1;1.8;2.5;0.2;0.9

See Also

| | | | |