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) carries one natively
typed cell per value, and a hidden __datatypes_meta__ sheet
carries the variable types, names, descriptions, and units needed to
restore the exact Octave types on read-back. Variables map to ODS cell
types as follows:
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.
string, categorical, and
calendarDuration variables become string cells.
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. Variable descriptions and units are
written whenever any variable has a non-empty description or unit,
respectively (the others are left empty). A zone-aware datetime
variable keeps its TimeZone on read-back.
table2ods (…, writes to a sheet
named name (default 'Sheet', name)'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.
table2ods (…, selects the
behaviour: 'WriteMode', mode)'overwritesheet' / 'inplace' replace the sheet
(the default when the sheet exists), 'append' appends the table’s
rows to it, and 'replacefile' discards any existing file.
Nested tables and structures are not supported and raise an error. Note
the following round-trip limitations when reading the file back with
ods2table: calendarDuration and categorical
variables are returned as cell arrays of character vectors (their values
are not reconstructed), and datetime and duration display formats are not
preserved, although the values themselves are exact.
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);