%% 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)

% 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

% 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)

% 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

% Copyright 2012 The MathWorks, Inc.