table

Methods

Method Reference: table.table2ods

table: table2ods (tbl, file)
table: table2ods (tbl, file, Name, Value)

Write a table to an OpenDocument spreadsheet file.

table2ods (tbl, file) writes the table tbl to file, which may be a character vector, a cellstr, or a string scalar. When file ends in .ods a compressed (ZIP-packaged) OpenDocument spreadsheet is written; when it ends in .fods a flat (single-XML) OpenDocument spreadsheet is written instead. The resulting file can be read back with ods2table.

The data sheet (named Sheet1 by default) is headed by the variable names and then carries one natively typed cell per value, and a hidden __datatypes_meta__ sheet carries the variable types, descriptions and units needed to restore the exact Octave types on read-back. The names are on the data sheet, where anyone opening the file can see them, and a nested table takes one header row per nesting level. Variables map to ODS cell types as follows:

  • Numeric variables become float cells and logical variables become boolean cells. Integers are written with their exact digits.
  • datetime variables become native date cells and duration variables become native time cells, both encoded as ISO 8601 strings.
  • Character, cellstr, string, categorical, and calendarDuration variables become string cells.
  • A multicolumn variable is split into consecutive columns that share the same variable name.

Missing values (NaN, NaT, and missing strings) are written as empty cells. When tbl has row names they are written under a leading RowNames column. A row of variable descriptions and a row of variable units are written whenever the corresponding property is set; a property that was never set writes no row at all, so that it reads back unset. A zone-aware datetime variable keeps its TimeZone on read-back.

The following Name-Value options are supported:

NameValue
'Sheet'The name of the sheet to write (default 'Sheet1'). When file already exists the named sheet is added or replaced while every other sheet is preserved, so a workbook can be built up one table at a time.
'WriteVariableNames'A logical scalar specifying whether the variable names are written (default true). When false the file carries none at all, the hidden metadata sheet included, so ods2table numbers the variables on read and can no longer group the columns: a multicolumn variable comes back as separate variables and a nested table as flat columns.
'WriteRowNames'A logical scalar specifying whether the row labels are written as a leading column (default true).
'WriteMode''overwritesheet' or 'inplace' replace the sheet (the default when the sheet exists), 'append' appends the table’s rows to it, and 'replacefile' discards any existing file.

Source Code: table

A nested table is split into columns tagged with both the outer and the nested variable name, and a structure into one column per field, exactly as table2csv does; the tagging rows live on the hidden metadata sheet, so the data sheet stays flat. Note the following round-trip limitation when reading the file back with ods2table: calendarDuration and categorical variables are returned as cell arrays of character vectors and their values are not reconstructed.

Source Code: table

table2ods writes a table to an OpenDocument spreadsheet, one natively typed cell per value. The data lands on Sheet1 by default.

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

    Age    Height    
    ___    ______    

     38        71    
     43        69    
     40        64
 filename = fullfile (tempdir (), 'patients.ods');
 table2ods (T, filename);
 ods2table (filename)
ans =
  3x2 table

    Age    Height    
    ___    ______    

     38        71    
     43        69    
     40        64

Writing to a named 'Sheet' of an existing workbook adds or replaces just that sheet, leaving the others intact — so several tables can share one file.

 filename = fullfile (tempdir (), 'workbook.ods');
 table2ods (table ([38; 43], 'VariableNames', {'Age'}), filename, 'Sheet', 'Patients');
 table2ods (table ([1; 2; 3], 'VariableNames', {'Visit'}), filename, 'Sheet', 'Visits');
 ods2table (filename, 'Sheet', 'Visits')
ans =
  3x1 table

    Visit    
    _____    

        1    
        2    
        3
 delete (filename);