writetable

Write table to file.

Syntax

  • writetable(T)

  • writetable(T, filename)

  • writetable(..., Name, Value)

Input argument

  • 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:

  • Text files: Each variable in T becomes a column, and variable names serve as column headers in the first line.

  • XML files: Each variable in T becomes an XML node, with variable names as element node names.

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

  • .txt, .dat, .csv: Delimited text files.

  • .xml: XML files.

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

  • WriteRowNames: Include row names in the output file (default: false).

  • FileType: Specify file format ('text' or 'xml').

  • WriteVariableNames: Include variable names as column headings in text files (default: true).

  • WriteMode: Specify writing mode ('overwrite' or 'append').

  • Delimiter: Define the field delimiter for text files (',', '\t', etc.).

  • QuoteStrings: Control how text is quoted in text files ('minimal', 'all', or 'none').

  • AttributeSuffix: Specify attribute suffix for XML files (default: 'Attribute').

  • RowNodeName: Specify XML row node names (default: 'row').

  • TableNodeName: Specify XML root node name (default: 'table').

Example

Examples demonstrating various usages of

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)

See also

table.

History

Version
Description

1.10.0

Initial version.

Author

Allan CORNET

Last updated