writetable
Write table to file.
📝Syntax
writetable(T)
writetable(T, filename)
writetable(..., Name, Value)
📥Input Arguments
Parameter Description
T A table to be written to a file.
filename A string specifying the destination filename.
📄Description

writetable(T) writes the table T to a comma-delimited text file.

The file name is derived from the table's workspace variable name, with the .txt extension appended.

If the file name cannot be derived from the table name, the default file name table.txt is used.

Output formats supported:

To specify the file name explicitly, use writetable(T, filename). The file format is determined by the file extension:

Additional options: Use writetable(..., Name, Value) for customization:

JSON files (.json extension or 'FileType', 'json'): the table is written as a JSON array with one object per row; keys are the variable names.

💡Examples
Examples demonstrating various usages of writetable.
T = table([1; 2; 3], {'A'; 'B'; 'C'}, [10.5; 20.7; 30.2], 'VariableNames', {'ID', 'Name', 'Value'});
T.Value_Attribute = {'High'; 'Medium'; 'Low'};

% Basic usage - write to text file
writetable(T)

% Write to specific CSV file with custom delimiter
writetable(T, 'data.csv', 'Delimiter', ';')

% Write to XML with custom node names
writetable(T, 'data.xml', 'RowNodeName', 'record', 'TableNodeName', 'dataset')

% Append to existing file with row names
writetable(T, 'data.txt', 'WriteMode', 'append', 'WriteRowNames', true)
Write a table to a JSON file:
T = table([1; NaN], ["a"; missing], [true; false], 'VariableNames', {'x', 'name', 'ok'}); f = [tempdir, 'writetable_json.json']; writetable(T, f); fileread(f) writetable(T, f, 'PrettyPrint', false, 'PreserveInfAndNaN', false); fileread(f)
🔗See Also
table
🕔Version History
Version Description
1.10.0 Initial version.
2.0.0 JSON files: FileType json, PrettyPrint and PreserveInfAndNaN.
Edit this page on GitHub