writetimetable
Write timetable to file
Description
writetimetable( writes the timetable
TT)TT to a comma-delimited text file. The file name is the workspace
variable name of the timetable, appended with the extension .txt. If
writetimetable cannot construct the file name from the input timetable
name, then it writes to the file timetable.txt.
Each column of each variable in TT becomes a column in the output
file. The variable names of TT become column headings in the first line
of the file. The writetimetable function overwrites any existing
file.
writetimetable(
writes to a file with the name and extension specified by
TT,filename)filename.
writetimetable determines the file format based on the specified
extension. The extension must be one of the following:
.txt,.dat, or.csvfor delimited text files.xls,.xlsm, or.xlsxfor Excel® spreadsheet files.xlsbfor Excel spreadsheet files supported on systems with Excel for Windows®.jsonfor JavaScript Object Notation (JSON) files.xmlfor Extensible Markup Language (XML) files
writetimetable(___,
specifies options using one or more name-value arguments in addition to any of the input
argument combinations in previous syntaxes. For example, you can specify whether to write
the variable names as column headings in the output file.Name=Value)
Examples
Create a timetable, write it to a comma-separated text file, and then write the timetable to another text file with a different delimiter character.
Define a timetable in the workspace with row times duration vector RowTimes.
RowTimes = seconds(1:5)'; TT = timetable(RowTimes,[98;97.5;97.9;98.1;97.9],[120;111;119;117;116],... 'VariableNames',{'Reading1','Reading2'})
TT=5×2 timetable
1 sec 98.0000 120
2 sec 97.5000 111
3 sec 97.9000 119
4 sec 98.1000 117
5 sec 97.9000 116
Write the timetable to a comma delimited text file and display the file contents. The writetimetable function outputs a text file named TT.txt.
writetimetable(TT)
type 'TT.txt'RowTimes,Reading1,Reading2 1 sec,98,120 2 sec,97.5,111 3 sec,97.9,119 4 sec,98.1,117 5 sec,97.9,116
To write the same timetable to a text file with a different delimiter character, use the 'Delimiter' name-value pair.
writetimetable(TT,'TT_bar.txt','Delimiter','bar') type 'TT_bar.txt'
RowTimes|Reading1|Reading2 1 sec|98|120 2 sec|97.5|111 3 sec|97.9|119 4 sec|98.1|117 5 sec|97.9|116
Create a timetable, write it to a spreadsheet file, and then display the contents of the file.
Define a timetable in the workspace with row times as a datetime vector.
Y = [2014:2018]'; M = ones(5,1); D = ones(5,1); RowTimes = datetime(Y,M,D); % Create Row Times TT = timetable(RowTimes,[98;97.5;97.9;98.1;97.9],[120;111;119;117;116],... 'VariableNames',{'Reading1','Reading2'})
TT=5×2 timetable
01-Jan-2014 98.0000 120
01-Jan-2015 97.5000 111
01-Jan-2016 97.9000 119
01-Jan-2017 98.1000 117
01-Jan-2018 97.9000 116
Write the table to spreadsheet file 'TT.xlsx'. By default, the writetimetable function writes the first worksheet in the file. Use the 'Sheet' name-value pair to specify the worksheet to write to.
writetimetable(TT,'TT.xlsx','Sheet',2)
Read and display the file contents.
readtimetable('TT.xlsx','Sheet',2)
ans=5×2 timetable
01-Jan-2014 00:00:00 98.0000 120
01-Jan-2015 00:00:00 97.5000 111
01-Jan-2016 00:00:00 97.9000 119
01-Jan-2017 00:00:00 98.1000 117
01-Jan-2018 00:00:00 97.9000 116
Input Arguments
Input timetable.
File name, specified as a character vector or string scalar.
Depending on the location you are writing to, filename can take
on one of these forms.
Location | Form | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Current folder | To write to the current folder, specify the name of the file in
Example:
| ||||||||
Other folders | To write to a folder different from the current folder, specify the
full or relative path name in
Example:
Example:
| ||||||||
Remote Location | To write to a remote location,
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
If
filenameincludes the file extension, then the writing function determines the file format from the extension. Otherwise, the writing function creates a comma separated text file and appends the extension.txt. Alternatively, you can specifyfilenamewithout the file’s extension, and then include the'FileType'name-value pair arguments to indicate the type of file.
If
filenamedoes not exist, then the writing function creates the file.If
filenameis the name of an existing text file, then the writing function overwrites the file.If
filenameis the name of an existing spreadsheet file, then the writing function writes the data to the specified location, but does not overwrite any values outside the range of the input data.
Data Types: char | string
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN, where Name is
the argument name and Value is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: writetimetable(TT,WriteVariableNames=false) specifies not to
include variable names as the first row of the output file.
Data Location
Worksheet to write to, specified as the comma-separated pair consisting of
'Sheet' and a character vector or a string scalar containing the
worksheet name or a positive integer indicating the worksheet index. The worksheet
name cannot contain a colon (:). To determine the names of sheets
in a spreadsheet file, use sheets = sheetnames(filename). For more
information, see sheetnames.
Specify the worksheet to write to by name or index:
name — If the specified sheet name does not exist in the file, then the writing function adds a new sheet at the end of the worksheet collection.
index — If the specified sheet index is an index larger than the number of worksheets, then the writing function appends empty sheets until the number of worksheets in the workbook equals the sheet index. The writing function also generates a warning indicating that it has added a new worksheet.
You can use the 'Sheet' name-value pair only with spreadsheet
files.
Example: 'Sheet',2
Example: 'Sheet',
'MySheetName'
Data Types: char | string | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Rectangular portion of worksheet to write to, specified as the comma-separated
pair consisting of 'Range' and a character vector or string scalar
in one of the following forms.
Form of the Value of Range
| Description |
|---|---|
' |
Example:
|
' |
Example:
|
The 'Range' name-value pair can only be used with Excel files.
Example: 'Range', 'A1:F10'
Data Types: char | string
XML root node name, specified as the comma-separated pair consisting of
'TableNodeName' and either a character vector or string scalar
the writing function writes as the root node name in the output XML file. If you do
not specify TableNodeName, then the writing function writes
'table' as the name of the root node in the output table.
Example: 'TableNodeName','RootName'
File Information
Type of file, specified as "text",
"spreadsheet", "json", or
"xml".
The FileType name-value argument must be used with the
filename input argument. You do not need to specify the
FileType name-value argument if the filename
input argument includes a standard file extension. The following standard file
extensions are recognized by the writing function:
.txt,.dat, or.csvfor delimited text files.xls,.xlsm, or.xlsxfor Excel spreadsheet files.xlsbfor Excel spreadsheet files supported on systems with Excel for Windows.jsonfor JavaScript Object Notation (JSON) files.xmlfor XML files
Data Types: char | string
Character encoding scheme associated with the file, specified as the
comma-separated pair consisting of 'Encoding' and
'system' or a standard character encoding scheme name. When you
do not specify any encoding, the writing function uses UTF-8 to write the file.
Example: 'Encoding','UTF-8' uses UTF-8 as the
encoding.
Data Types: char | string
Flag to start an instance of Microsoft
Excel for Windows when writing spreadsheet data, specified as the comma-separated pair
consisting of 'UseExcel' and either true, or
false.
You can set the 'UseExcel' parameter to one of these values:
true— The writing function starts an instance of Microsoft Excel when writing the file.false— The writing function does not start an instance of Microsoft Excel when writing the file. When operating in this mode, functionality for writing differs in the support of file formats and interactive features, such as formulas and macros.
UseExcel |
|
|
|---|---|---|
| Supported file formats |
|
|
Support for interactive features, such as formulas and macros | Yes | No |
When writing to spreadsheet files on Windows platforms, if you want to start an instance of Microsoft
Excel, then set the 'UseExcel' parameter to
true.
UseExcel is not supported in noninteractive, automated
environments.
Writing mode, specified as the comma-separated pair consisting of
'WriteMode' and a character vector or a string scalar. Select a
write mode based on the file type.
File Type | Write Mode |
|---|---|
| Text Files |
If the file you specified does not exist, then the writing function creates and writes data to a new file. |
| Spreadsheet Files |
|
When
WriteVariableNamesis set totrue, the writing function does not support the write mode'append'.For spreadsheet files:
When the write mode is
'append', the writing function does not support theRangeparameter.If the file you specified does not exist, then the writing function performs the same actions as
'replacefile'.
Example: 'WriteMode','append'
Data Types: char | string
Variables and Rows
Indicator for writing variable names as column headings, specified as the
comma-separated pair consisting of 'WriteVariableNames' and either
true or false.
Indicator | Behavior |
|---|---|
| The writing function includes variable names as the column headings of the output. This is the default behavior. |
| The writing function does not include variable names in the output. |
XML row node name, specified as the comma-separated pair consisting of
'RowNodeName' and either a character vector or string scalar the
writing function writes as the node name in the output XML file that corresponds to
the rows in the input table. If you do not specify RowNodeName,
then the writing function writes 'row' as the name of the row nodes
in the output table.
Example: 'TableNodeName','RootName'
Data Formatting
Locale for writing dates, specified as a string scalar or character vector of the
form , where:xx_YY
xxis a lowercase ISO 639-1 two-letter code indicating a language.YYis an uppercase ISO 3166-1 alpha-2 code indicating a country.
Use DateLocale to specify the locale in which
writetimetable writes month and day-of-week names and
abbreviations. However, the function ignores the DateLocale value
whenever dates can be written as Excel-formatted dates.
This table lists some common values for the locale.
| Locale | Language | Country |
|---|---|---|
"de_DE" | German | Germany |
"en_GB" | English | United Kingdom |
"en_US" | English | United States |
"es_ES" | Spanish | Spain |
"fr_FR" | French | France |
"it_IT" | Italian | Italy |
"ja_JP" | Japanese | Japan |
"ko_KR" | Korean | Korea |
"nl_NL" | Dutch | Netherlands |
"zh_CN" | Chinese (simplified) | China |
Example: DateLocale="ja_JP"
Indicator for writing quoted text, specified as "minimal",
"all", or "none".
If
QuoteStringsis"minimal", then the function encloses any variables containing the delimiter, line ending, or double-quote character in double-quote characters.If
QuoteStringsis"all", then the function encloses all text, categorical, datetime, and duration variables in double-quote characters.If
QuoteStringsis"none", then the function does not enclose variables in double-quote characters.
You can use the QuoteStrings name-value argument only with
delimited text files.
Since R2026a
Preserve Inf and NaN values in the output
JSON file, specified as a numeric or logical 1
(true) or 0 (false). By
default, writetimetable writes Inf and
NaN values to the output JSON file. Specify this argument as
false to write all Inf and
NaN values as JSON null values.
Example: PreserveInfAndNaN=false
File Formatting
Field delimiter character, specified as the comma-separated pair consisting of
'Delimiter' and a character vector or string scalar containing
one of these specifiers:
Specifier | Field Delimiter |
|---|---|
| Comma. This is the default behavior. |
| Space |
| Tab |
| Semicolon |
| Vertical bar |
You can use the 'Delimiter' name-value pair only for delimited
text files.
Example: 'Delimiter','space'
Data Types: char | string
Attribute suffix, specified as the comma-separated pair consisting of
'AttributeSuffix' and either a character vector or string scalar
indicating which variable names in the input table to write as attributes in the
output XML file.
For example, for a variable name AttName_att in the input
table, you can specify 'AttributeSuffix','_att' to indicate that
'AttName' should be written out as an attribute in the output XML
file.
If you do not specify
'AttributeSuffix',writetimetabledefaults to writing variables with the suffix'Attribute'as attributes in the output XML file.If the attribute specified as the value of
'AttributeSuffix'matches the suffix appended to the variable name in the input table, the suffix will be dropped from the variable name in the output XML file. For example, if you specify'AttributeSuffix','_att', a variable in the input table namedMyField_attwill correspond to the attribute namedMyFieldin the XML file.
Example: 'AttributeSuffix','_att'
Preserve cell formatting of existing spreadsheet, specified as
true or false. If you specify
false, writetimetable will not preserve the
cell formatting of the spreadsheet. Formatting includes elements such as fonts, cell
borders, and color-shaded cells.
When writing datetime data to a spreadsheet file, you must set
both 'PreserveFormat' and the 'UseExcel'
Name-Value pair to true to preserve the existing cell formatting.
If 'UseExcel' is set to false and
'PreserveFormat' is set to true when writing
datetime data to the file, writetimetable will
not preserve the existing cell formatting of the file.
Example: 'PreserveFormat',false
Automatically adjust column width, specified as true or
false. If you specify a value of 0 or
false, then writetimetable will not
automatically adjust the column widths to fit the data in the cells.
Example: 'AutoFitWidth',0
Since R2026a
Indent text in the output JSON file, specified as a numeric or logical
1 (true) or 0
(false). By default, writetimetable formats
text in the JSON file using an indentation of four spaces. By default,
writetimetable formats text in the JSON file using an indentation
of four spaces. If you specify PrettyPrint as
false, writetimetable writes the JSON text
without indentations or new lines.
Algorithms
There are some instances where the
writetimetablefunction creates a file that does not represent the input data exactly. You will notice this when you use thereadtimetablefunction to read that file. The resulting data might not have the exact same format or contents as the original data. If you need to save your timetable and retrieve it at a later time to exactly match the original timetable, with the same data and organization, then save it as a MAT-file.writetimetablewrites an inexact table in the following instances:writetimetablewrites out numeric data usinglong gformat, and categorical or character data as unquoted text.writetimetablewrites out variables that have more than two dimensions as two dimensional variables, with the trailing dimensions collapsed.For cell-valued variables,
writetimetablewrites out the variables differently based on the data type of the content in the cells.If the content of the cell is of the data type numeric, text, logical, categorical, datetime, or duration, then the
writetimetablefunction writes out the contents of each cell as a single row, in multiple, delimiter-separated fields.Otherwise, the
writetimetablefunction writes out a single empty field.
Excel converts
Infvalues to65535. MATLAB® convertsNaN,NaT,<undefined>categorical values, and<missing>string values to empty cells.
Version History
Introduced in R2019aWrite timetable data to a JSON file. Specify optional name-value arguments to control
the export behavior. For example, set PrettyPrint to
false if you want to write data to a JSON file without formatting it
using indentations or new lines.
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)