table

Methods

Method Reference: table.writetable

table: writetable (tbl, filename)
table: writetable (tbl, filename, Name, Value)

Write a table to a file in a MATLAB-compatible format.

writetable (tbl, filename) writes the table tbl to filename. The file type is inferred from the extension: .txt, .csv, and .dat are written as delimited text; .ods as an OpenDocument spreadsheet; and .xlsx and .xlsm as Excel spreadsheets. Use the 'FileType' option to override the inferred type.

Unlike table2csv/table2ods, no type metadata is written: the file holds only an optional variable-name header row followed by the data, so it can be read by other applications. Type information is recovered by readtable through automatic detection (text) or the native cell types (spreadsheet). The following options are supported:

NameValue
'FileType''text' or 'spreadsheet'.
'WriteVariableNames'Logical; write the variable names as the first row (default true).
'WriteRowNames'Logical; write the row names as the first column (default false).
'Delimiter'Field delimiter for text files: a single character or one of 'comma', 'space', 'tab', 'semi', 'bar' (default ',').
'QuoteStrings''minimal', 'all', or 'none' for text files (default 'minimal').
'Sheet'Spreadsheet only: the name of the sheet to write. The default is the first sheet of an existing workbook, or 'Sheet1' for a new file.
'Range'Spreadsheet only: an A1-style anchor such as 'C5' at which to place the top-left corner of the data (fresh writes only).
'WriteMode'For text: 'overwrite' (default) or 'append'. For spreadsheets: 'overwritesheet' / 'inplace' (replace the target sheet), 'append' (append rows to it), or 'replacefile' (overwrite the whole file).

Source Code: table

When the target spreadsheet already exists, the sheet named by 'Sheet' (defaulting to the first existing sheet) is added or replaced while every other sheet is preserved, unless 'WriteMode' is 'replacefile'. For ODS, existing foreign spreadsheets (for example those written by LibreOffice) are updated in place, keeping their other parts; for Excel (.xlsx, .xlsm) the workbook is read back and rewritten, so only its cell values are preserved.

Nested tables and structures are not supported, and the legacy binary formats .xls and .xlsb are not supported either; use .xlsx, .ods, or a text format.

Source Code: table

Example: 1

writetable writes a table in a MATLAB-interoperable format — a plain header row of variable names followed by the data, with no package-specific metadata. The file type follows the extension; 'Delimiter' sets the separator for text files.

 T = table ([38; 43; 40], [71; 69; 64], 'VariableNames', {'Age', 'Height'})
T =
  3x2 table

    Age    Height    
    ___    ______    

     38        71    
     43        69    
     40        64
 filename = fullfile (tempdir (), 'patients.txt');
 writetable (T, filename, 'Delimiter', 'tab');
 type (filename)
Age	Height
38	71
43	69
40	64
 delete (filename);

Example: 2

For spreadsheets, choose the target 'Sheet' and, with 'WriteRowNames', emit the row names as the leading column — readtable reads it straight back.

 T = table ([38; 43], [71; 69], 'VariableNames', {'Age', 'Height'}, ...
            'RowNames', {'Li', 'Diaz'})
T =
  2x2 table

            Age    Height    
            ___    ______    

    Li       38        71    
    Diaz     43        69
 filename = fullfile (tempdir (), 'patients.ods');
 writetable (T, filename, 'Sheet', 'Cohort', 'WriteRowNames', true);
 readtable (filename, 'Sheet', 'Cohort', 'ReadRowNames', true)
ans =
  2x2 table

            Age    Height    
            ___    ______    

    Li       38        71    
    Diaz     43        69
 delete (filename);