readtable
Create table from file
Description
creates
a table by reading column oriented data from a file.T
= readtable(filename
)
readtable
determines the file format from
the file extension:
.txt
,.dat
, or.csv
for delimited text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
for Extensible Markup Language (XML) files.docx
for Microsoft® Word document files.html
,.xhtml
, or.htm
for Hypertext Markup Language (HTML) files
For text and spreadsheet files, readtable
creates one variable in
T
for each column in the file and reads variable names from the first row of
the file. For XML files, readtable
creates one variable in
T
for each element or attribute node detected as a table variable. Variable
names correspond to element and attribute names. For Microsoft Word document files, readtable
, by default, imports data from the
first table in the document and creates one variable in T
for each column in
the file and reads variable names from the first row of the table. For HTML files,
readtable
, by default, imports data from the first
<TABLE>
element and creates one variable in T
for each
column in the file and reads variable names from the first row of the table.
By default, readtable
creates variables that have data types that are
appropriate for the data values detected in each column of the input file.
creates a table from a file with additional options specified by one or more name-value pair
arguments. For example, you can specify whether T
= readtable(___,Name,Value
)readtable
reads the first row
of the file as variable names or as data.
To set specific import options for your data, you can either use the
opts
object or you can specify name-value pairs. When you specify name-value
pairs in addition to opts
, then readtable
supports only
these name-value pairs:
Text and spreadsheet parameters —
ReadVariableNames
,ReadRowNames
Text only parameters —
DateLocale
,Encoding
Spreadsheet only parameters —
Sheet
,UseExcel
Examples
Create Table from Text File
Load the file myCsvTable.dat
and preview its contents in a text editor. A screen shot is shown below. Notice that the file contains comma-separated column oriented data.
filename = 'myCsvTable.dat';
Create a table from the comma-separated text file. The resulting table T
contains one variable for each column in the file and readtable
treats the entries in the first line of the file as variable names.
T = readtable(filename)
T=5×6 table
LastName Gender Age Height Weight Smoker
____________ ______ ___ ______ ______ ______
{'Smith' } {'M'} 38 71 176 1
{'Johnson' } {'M'} 43 69 163 0
{'Williams'} {'F'} 38 64 131 0
{'Jones' } {'F'} 40 67 133 0
{'Brown' } {'F'} 49 64 119 0
Ignore Headers and Fill Missing Values
Starting in R2020a, the readtable
function read an input file as though it automatically called the detectImportOptions
function on the file. It can detect data types, discard extra header lines, and fill in missing values.
For example, preview the file headersAndMissing.txt
in a text editor. The file has a line with column names and another line with headers. The last two rows have gaps where the previous rows have data values.
Create a table from the file. The readtable
function discards the headers. Also, it fills in gaps with appropriate missing values—a NaN
value for numeric variables, and an empty character vector for text.
T = readtable('headersAndMissing.txt')
T=5×6 table
LastName Gender Age Height Weight Smoker
___________ __________ ___ ______ ______ ______
{'Wu' } {'M' } 38 71 176 1
{'Johnson'} {'M' } 43 69 163 0
{'Sanchez'} {'F' } 38 64 131 0
{'Brown' } {'F' } NaN 67 133 0
{'Picard' } {0x0 char} NaN 64 119 0
To restore the default behavior from previous releases, specify the 'Format','auto'
name-value pair argument. readtable
reads the headers as data, and as a result, it converts all the table variables to text.
T = readtable('headersAndMissing.txt','Format','auto')
T=6×6 table
LastName Gender Age Height Weight Smoker
___________ __________ __________ _______ _______ ___________
{'string' } {'string'} {'int' } {'int'} {'int'} {'boolean'}
{'Wu' } {'M' } {'38' } {'71' } {'176'} {'1' }
{'Johnson'} {'M' } {'43' } {'69' } {'163'} {'0' }
{'Sanchez'} {'F' } {'38' } {'64' } {'131'} {'0' }
{'Brown' } {'F' } {0x0 char} {'67' } {'133'} {'0' }
{'Picard' } {0x0 char} {0x0 char} {'64' } {'119'} {'0' }
For more information, see the Compatibility Considerations section on this page.
Create Table from Text File Without Column Headings
Preview the file mySpaceDelimTable.txt
in a text editor. A screen shot is shown below. Notice that the file contains space delimited, column oriented data.
Create a table from the space delimited text file. The readtable
function assigns the default variable names Var1
to Var5
because the file does not contain detectable column names in its first row.
T = readtable('mySpaceDelimTable.txt')
T=3×5 table
Var1 Var2 Var3 Var4 Var5
_____ ____ ____ ______ _________
{'M'} 45 45 {'NY'} {'true' }
{'F'} 41 32 {'CA'} {'false'}
{'M'} 40 34 {'MA'} {'false'}
Create and Format Table from Text File
Load the file myCsvTable.dat
and preview its contents in a text editor. A screen shot is shown below. Notice that the file contains comma-separated column oriented data.
filename = 'myCsvTable.dat';
Create a table from the comma-separated text file. Import the first two columns as character vectors, the third column as uint32
, and the next two columns as double-precision, floating-point numbers. Import the entries of the last column as character vectors.
T = readtable(filename,'Format','%s%s%u%f%f%s')
T=5×6 table
LastName Gender Age Height Weight Smoker
____________ ______ ___ ______ ______ ______
{'Smith' } {'M'} 38 71 176 {'1'}
{'Johnson' } {'M'} 43 69 163 {'0'}
{'Williams'} {'F'} 38 64 131 {'0'}
{'Jones' } {'F'} 40 67 133 {'0'}
{'Brown' } {'F'} 49 64 119 {'0'}
The conversion specifiers are %s
for a cell array of character vectors, %f
for double
, and %u
for uint32
.
Read Foreign-Language Dates from Text File
Read German dates from a file and add them to a table as English dates.
Preview the file german_dates.txt
in a text editor. A screen shot is shown below. Notice that the first column of values contains dates in German and the second and third columns are numeric values.
Read the sample file using readtable
. The conversion specifiers is %D
dates and %f
for floating-point values. Specify the file encoding using the FileEncoding
name-value pair argument. Specify the format and locale of the dates using the DateLocale
name-value pair argument.
T = readtable('german_dates.txt',... 'Format','%{dd MMMM yyyy}D %f %f',... 'FileEncoding','ISO-8859-15',... 'DateLocale','de_DE')
T=3×3 table
Var1 Var2 Var3
________________ ____ _____
01 January 2014 20.2 100.5
01 February 2014 21.6 102.7
01 March 2014 20.7 99.8
Create Table from Spreadsheet Including Row Names
Create a table from a spreadsheet that contains variable names in the first row and row names in the first column.
T = readtable('patients.xls','ReadRowNames',true);
Display the first five rows and first four variables of the table.
T(1:5,1:4)
ans=5×4 table
Gender Age Location Height
__________ ___ _____________________________ ______
Smith {'Male' } 38 {'County General Hospital' } 71
Johnson {'Male' } 43 {'VA Hospital' } 69
Williams {'Female'} 38 {'St. Mary's Medical Center'} 64
Jones {'Female'} 40 {'VA Hospital' } 67
Brown {'Female'} 49 {'County General Hospital' } 64
View the DimensionNames
property of the table.
T.Properties.DimensionNames
ans = 1x2 cell
{'LastName'} {'Variables'}
'LastName'
is the name in the first column of the first row of the spreadsheet.
Read Specific Range of Data from Spreadsheet
Create a table using data from a specified region of the spreadsheet patients.xls
. Use the data from the 5-by-3 rectangular region between the corners C2
and E6
. Do not use the first row of this region as variable names.
T = readtable('patients.xls',... 'Range','C2:E6',... 'ReadVariableNames',false)
T = Var1 Var2 Var3 ____ ___________________________ ____ 38 'County General Hospital' 71 43 'VA Hospital' 69 38 'St. Mary's Medical Center' 64 40 'VA Hospital' 67 49 'County General Hospital' 64
T
contains default variable names.
Detect and Use Import Options for Text Files
Create import options, tailor the data types for multiple variables, and then read the data.
Create an import options object from a text file.
opts = detectImportOptions('airlinesmall.csv')
opts = DelimitedTextImportOptions with properties: Format Properties: Delimiter: {','} Whitespace: '\b\t ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'ISO-8859-1' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more} VariableTypes: {'double', 'double', 'double' ... and 26 more} SelectedVariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more} VariableOptions: Show all 29 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [2 Inf] VariableNamesLine: 1 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Examine the Type
property of the variables TaxiIn
and TaxiOut
.
getvaropts(opts,{'TaxiIn','TaxiOut'})
ans = 1x2 TextVariableImportOptions array with properties: Name Type FillValue TreatAsMissing QuoteRule Prefixes Suffixes EmptyFieldRule WhitespaceRule
Change the type of the variables TaxiIn
and TaxiOut
to double
.
opts = setvartype(opts,{'TaxiIn','TaxiOut'},'double');
Specify the subset of variables to import and examine.
opts.SelectedVariableNames = {'TaxiIn','TaxiOut'};
Use the readtable
function along with the options object to import the selected variables. Display a summary of the table.
T = readtable('airlinesmall.csv',opts);
summary(T)
Variables: TaxiIn: 123523x1 double Values: Min 0 Median 5 Max 1451 NumMissing 37383 TaxiOut: 123523x1 double Values: Min 0 Median 13 Max 755 NumMissing 37364
Detect and Use Import Options for Spreadsheet Files
Detect import options for a spreadsheet file, specify the variables to import, and then read the data.
Create an import options object from a file.
opts = detectImportOptions('patients.xls')
opts = SpreadsheetImportOptions with properties: Sheet Properties: Sheet: '' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' Variable Import Properties: Set types by name using setvartype VariableNames: {'LastName', 'Gender', 'Age' ... and 7 more} VariableTypes: {'char', 'char', 'double' ... and 7 more} SelectedVariableNames: {'LastName', 'Gender', 'Age' ... and 7 more} VariableOptions: Show all 10 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Range Properties: DataRange: 'A2' (Start Cell) VariableNamesRange: 'A1' RowNamesRange: '' VariableUnitsRange: '' VariableDescriptionsRange: '' To display a preview of the table, use preview
Modify the options object to specify which variables to import.
opts.SelectedVariableNames = {'Systolic','Diastolic'};
Use readtable
along with the options object to import the specified variables.
T = readtable('patients.xls',opts);
summary(T)
Variables: Systolic: 100x1 double Values: Min 109 Median 122 Max 138 Diastolic: 100x1 double Values: Min 68 Median 81.5 Max 99
Read Tabular Data Containing Arbitrary Variable Names
You can read tabular data while preserving variable names that include any characters, including spaces and non-ASCII characters. First, create a table with arbitrary variable names and write the table to a text file. Then, read the tabular data back while preserving the original variable names.
Create a table containing arbitrary variable names and write the tabular data to the file 'sampletable.txt'
.
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'}; Age = [38;43;38;40;49]; Smoker = logical([1;0;1;0;1]); varNames = {'Last Name','Age','Smoker (1 or 0)'}; T = table(LastName,Age,Smoker,'VariableNames',varNames)
T=5×3 table
Last Name Age Smoker (1 or 0)
___________ ___ _______________
{'Sanchez'} 38 true
{'Johnson'} 43 false
{'Li' } 38 true
{'Diaz' } 40 false
{'Brown' } 49 true
writetable(T,'sampletable.txt')
Read the tabular data back using readtable
. By default, MATLAB® converts any variable names that include spaces and non-ASCII characters into valid MATLAB® identifiers. For example, MATLAB® converts the variable names 'Last Name'
to 'LastName'
, and 'Smoker (1 or 0)'
to 'Smoker_1or0_'
. To read the tabular data while preserving variable names, set the 'VariableNamingRule'
parameter to preserve
.
T_preserve = readtable('sampletable.txt',"VariableNamingRule","preserve")
T_preserve=5×3 table
Last Name Age Smoker (1 or 0)
___________ ___ _______________
{'Sanchez'} 38 1
{'Johnson'} 43 0
{'Li' } 38 1
{'Diaz' } 40 0
{'Brown' } 49 1
Read Hexadecimal and Binary Numbers from File
You can read hexadecimal and binary numbers from a file and store them as numeric variables in a table. The readtable
function automatically reads hexadecimal and binary numbers when they include the 0x
and 0b
prefixes respectively. The numeric values are stored using integer data types. You can also use import options to read such numbers when they do not have prefixes.
Preview the hexAndBinary.txt
file in a text editor. It has columns of hexadecimal and binary numbers with prefixes, and one column without.
Read the file using readtable
. The function detects the numbers with 0x
and 0b
prefixes and stores them as integers. The third column does not have prefixes so its values are treated as text.
T = readtable('hexAndBinary.txt')
T=3×4 table
Var1 Var2 Var3 Var4
_____ ____ ________ ___________
255 255 {'C7F9'} {'Device1'}
256 4 {'05BF'} {'Device2'}
43981 129 {'F00F'} {'Device3'}
The readtable
function stores the numeric values in different integer classes (uint16
and uint8
) because T.Var1
has a value that requires more than 8 bits of storage.
class(T.Var1)
ans = 'uint16'
class(T.Var2)
ans = 'uint8'
To specify the data types for storing the numeric values imported from hexadecimal and binary numbers, use the 'HexType'
and 'BinaryType'
name-value pair arguments. Store the values as signed 32-bit integers.
T = readtable('hexAndBinary.txt','HexType','int32','BinaryType','int32'); class(T.Var1)
ans = 'int32'
class(T.Var2)
ans = 'int32'
You can use import options to detect hexadecimal and binary numbers without prefixes, and specify storage for them. Create an import options object for hexAndBinary.txt
.
opts = detectImportOptions('hexAndBinary.txt')
opts = DelimitedTextImportOptions with properties: Format Properties: Delimiter: {','} Whitespace: '\b\t ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more} VariableTypes: {'auto', 'auto', 'char' ... and 1 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more} VariableOptions: Show all 4 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [1 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
To specify that the third column should be imported as hexadecimal values, despite the lack of a prefix, use the setvaropts
function to modify the variable type for the third variable of the table. Set the variable type to int32
.
opts = setvaropts(opts,3,'NumberSystem','hex','Type','int32')
opts = DelimitedTextImportOptions with properties: Format Properties: Delimiter: {','} Whitespace: '\b\t ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more} VariableTypes: {'auto', 'auto', 'int32' ... and 1 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 1 more} VariableOptions: Show all 4 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [1 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
Read the file and import the third column as numeric values, not text.
T = readtable('hexAndBinary.txt',opts)
T=3×4 table
Var1 Var2 Var3 Var4
_____ ____ _____ ___________
255 255 51193 {'Device1'}
256 4 1471 {'Device2'}
43981 129 61455 {'Device3'}
Create XML Import Options for XML File
Create XML import options for an XML file, specify the variables to import, and then read the data.
The XML file students.xml
has four sibling nodes named Student
, which each contain the same child nodes and attributes.
type students.xml
<?xml version="1.0" encoding="utf-8"?> <Students> <Student ID="S11305"> <Name FirstName="Priya" LastName="Thompson" /> <Age>18</Age> <Year>Freshman</Year> <Address> <Street xmlns="https://www.mathworks.com">591 Spring Lane</Street> <City>Natick</City> <State>MA</State> </Address> <Major>Computer Science</Major> <Minor>English Literature</Minor> </Student> <Student ID="S23451"> <Name FirstName="Conor" LastName="Cole" /> <Age>18</Age> <Year>Freshman</Year> <Address> <Street xmlns="https://www.mathworks.com">4641 Pearl Street</Street> <City>San Francisco</City> <State>CA</State> </Address> <Major>Microbiology</Major> <Minor>Public Health</Minor> </Student> <Student ID="S119323"> <Name FirstName="Morgan" LastName="Yang" /> <Age>21</Age> <Year>Senior</Year> <Address> <Street xmlns="https://www.mathworks.com">30 Highland Road</Street> <City>Detriot</City> <State>MI</State> </Address> <Major>Political Science</Major> </Student> <Student ID="S201351"> <Name FirstName="Salim" LastName="Copeland" /> <Age>19</Age> <Year>Sophomore</Year> <Address> <Street xmlns="https://www.mathworks.com">3388 Moore Avenue</Street> <City>Fort Worth</City> <State>TX</State> </Address> <Major>Business</Major> <Minor>Japanese Language</Minor> </Student> <Student ID="S201351"> <Name FirstName="Salim" LastName="Copeland" /> <Age>20</Age> <Year>Sophomore</Year> <Address> <Street xmlns="https://www.mathworks.com">3388 Moore Avenue</Street> <City>Fort Worth</City> <State>TX</State> </Address> <Major>Business</Major> <Minor>Japanese Language</Minor> </Student> <Student ID="54600"> <Name FirstName="Dania" LastName="Burt" /> <Age>22</Age> <Year>Senior</Year> <Address> <Street xmlns="https://www.mathworks.com">22 Angie Drive</Street> <City>Los Angeles</City> <State>CA</State> </Address> <Major>Mechanical Engineering</Major> <Minor>Architecture</Minor> </Student> <Student ID="453197"> <Name FirstName="Rikki" LastName="Gunn" /> <Age>21</Age> <Year>Junior</Year> <Address> <Street xmlns="https://www.mathworks.com">65 Decatur Lane</Street> <City>Trenton</City> <State>ME</State> </Address> <Major>Economics</Major> <Minor>Art History</Minor> </Student> </Students>
Create an XMLImportOptions
object from a file. Specify the value of VariableSelectors
as //@FirstName
to select the FirstName
element node to import as a table variable.
opts = xmlImportOptions("VariableSelectors","//@FirstName")
opts = XMLImportOptions with properties: Replacement Properties: MissingRule: "fill" ImportErrorRule: "fill" RepeatedNodeRule: "addcol" Variable Import Properties: Set types by name using setvartype VariableNames: "Var1" VariableTypes: "char" SelectedVariableNames: "Var1" VariableOptions: Show all 1 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: "preserve" Location Properties: TableSelector: <missing> RowSelector: <missing> VariableSelectors: "//@FirstName" VariableUnitsSelector: <missing> VariableDescriptionsSelector: <missing> RowNamesSelector: <missing> RegisteredNamespaces: [0x2 string]
Use readtable
along with the options object to import the specified variable.
T = readtable("students.xml",opts)
T=7×1 table
Var1
__________
{'Priya' }
{'Conor' }
{'Morgan'}
{'Salim' }
{'Salim' }
{'Dania' }
{'Rikki' }
Register Custom XML Namespace Prefix
Register a custom XML namespace prefix to the existing namespace URL in the input file using the RegisteredNamespaces
name-value argument.
Create an XMLImportOptions
object from an XML file. Specify the XPath expression of the Street
element node as the value of 'VariableSelectors'
, and register the prefix myPrefix
to the URL belonging to the Street
node.
opts = detectImportOptions("students.xml","RegisteredNamespaces", ["myPrefix","https://www.mathworks.com"],... "VariableSelectors","//myPrefix:Street")
opts = XMLImportOptions with properties: Replacement Properties: MissingRule: "fill" ImportErrorRule: "fill" RepeatedNodeRule: "addcol" Variable Import Properties: Set types by name using setvartype VariableNames: "Street" VariableTypes: "string" SelectedVariableNames: "Street" VariableOptions: Show all 1 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: "preserve" Location Properties: TableSelector: <missing> RowSelector: <missing> VariableSelectors: "//myPrefix:Street" VariableUnitsSelector: <missing> VariableDescriptionsSelector: <missing> RowNamesSelector: <missing> RegisteredNamespaces: ["myPrefix" "https://www.mathworks.com"]
Use the readtable
function along with the options object to import the selected variable.
T2 = readtable("students.xml",opts)
T2=7×1 table
Street
___________________
"591 Spring Lane"
"4641 Pearl Street"
"30 Highland Road"
"3388 Moore Avenue"
"3388 Moore Avenue"
"22 Angie Drive"
"65 Decatur Lane"
Read Specific Table from Microsoft Word Document
The readtable
function, by default, reads the first table from a Microsoft Word document file. The file MaintenanceReport.docx
contains two tables. The last row of the second table contains a cell with merged columns that do not match the table variables.
Read the second table by setting the TableIndex
option to 2
. To skip rows that have cells with merged columns, set the MergedCellColumnRule
option to 'omitrow'
.
filename = "MaintenanceReport.docx"; T = readtable(filename,'TableIndex',2,'MergedCellColumnRule','omitrow')
T=3×5 table
Description Category Urgency Resolution Cost
_____________________________________________________________________ ____________________ ________ __________________ ________
"Items are occasionally getting stuck in the scanner spools." "Mechanical Failure" "Medium" "Readjust Machine" "$45"
"Loud rattling and banging sounds are coming from assembler pistons." "Mechanical Failure" "Medium" "Readjust Machine" "$35"
"There are cuts to the power when starting the plant." "Electronic Failure" "High" "Full Replacement" "$16200"
Alternatively, you can select a table using an XPath selector using the TableSelector
option. To select the Microsoft Word document table that contains the text "Description", use the XPath selector "//w:tbl[contains(.,'Description')]"
.
T = readtable(filename, ... 'TableSelector',"//w:tbl[contains(.,'Description')]", ... 'MergedCellColumnRule','omitrow')
T=3×5 table
Description Category Urgency Resolution Cost
_____________________________________________________________________ ____________________ ________ __________________ ________
"Items are occasionally getting stuck in the scanner spools." "Mechanical Failure" "Medium" "Readjust Machine" "$45"
"Loud rattling and banging sounds are coming from assembler pistons." "Mechanical Failure" "Medium" "Readjust Machine" "$35"
"There are cuts to the power when starting the plant." "Electronic Failure" "High" "Full Replacement" "$16200"
Read Table from HTML Webpage
Read the first table from the URL https://www.mathworks.com/help/matlab/text-files.html that contains the text "readtable"
using the XPath selector "//TABLE[contains(.,'readtable')]"
. The table does not have a header row, so set the ReadVariableNames
option to false
.
url = "https://www.mathworks.com/help/matlab/text-files.html"; T = readtable(url,'TableSelector',"//TABLE[contains(.,'readtable')]", ... 'ReadVariableNames',false)
T=4×2 table
Var1 Var2
________________ ____________________________
"readtable" "Create table from file"
"writetable" "Write table to file"
"readtimetable" "Create timetable from file"
"writetimetable" "Write timetable to file"
Input Arguments
filename
— Name of file to read
character vector | string scalar
Name of the file to read, specified as a character vector or a string scalar.
Depending on the location of your file, filename
can take on one of
these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder or folder on the MATLAB® path | Specify the name of the file in
Example:
| ||||||||
File in a folder | If the file is not in the current folder or in a folder on the
MATLAB path, then specify the full or relative path name in
Example:
Example:
| ||||||||
Internet URL | If the file is specified as an internet uniform resource locator (URL),
then Example:
| ||||||||
Remote Location | If the file is stored at a remote location, then
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
If
filename
includes the file extension, then the importing function determines the file format from the extension. Otherwise, you must specify the'FileType'
name-value pair arguments to indicate the type of file.On Windows® systems with Microsoft Excel® software, the importing function reads any Excel spreadsheet file format recognized by your version of Excel.
If your system does not have Excel for Windows or if you are using MATLAB Online™, the importing function operates with the
UseExcel
property set tofalse
, and reads only.xls, .xlsx, .xlsm, .xltx, and .xltm
files.For delimited text files, the importing function converts empty fields in the file to either
NaN
(for a numeric variable) or an empty character vector (for a text variable). All lines in the text file must have the same number of delimiters. The importing function ignores insignificant white space in the file.
Data Types: char
| string
opts
— File import options
SpreadsheetImportOptions
| DelimitedtextImportOptions
| FixedWidthImportOptions
| XMLImportOptions
| WordDocumentImportOptions
| HTMLDocumentImportOptions
File import options, specified as a SpreadsheetImportOptions
,
DelimitedTextImportOptions
, FixedWidthImportOptions
,
XMLImportOptions
, WordDocumentImportOptions
, or
HTMLDocumentImportOptions
object created by the detectImportOptions
function. The opts
object contains
properties that control the data import process. For more information on the properties of
each object, see the appropriate object page.
Type of Files | Output |
---|---|
Spreadsheet files | SpreadsheetImportOptions object (only
available for the Sheet , DataRange , and
VariableNames properties) |
Text files | DelimitedTextImportOptions object |
Fixed-width text files | FixedWidthImportOptions object |
XML files | XMLImportOptions
object |
Microsoft Word document files | WordDocumentImportOptions object |
HTML files | HTMLImportOptions object |
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.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: 'ReadVariableNames',false
indicates
that the first row of the file does not correspond to variable names.
FileType
— Type of file
'spreadsheet'
| 'text'
| 'delimitedtext'
| 'fixedwidth'
| 'xml'
| 'worddocument'
| 'html'
Type of file, specified as the comma-separated pair consisting of
'FileType'
and one of these values.
Value | Import Options |
---|---|
'spreadsheet' | |
'text' |
|
'delimitedtext' |
|
'fixedwidth' |
|
'xml' |
|
'worddocument' |
|
'html' |
|
Use the 'FileType'
name-value pair argument when
filename
does not include the file extension, or when the extension is
not one of these:
.txt
,.dat
, or.csv
for text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
, for XML files.docx
for Microsoft Word document files.html
,.xhtml
, or.htm
for HTML files
Example: 'FileType','text'
Data Types: char
| string
ReadRowNames
— Indicator for reading the first column as row names
false
(default) | true
| 0
| 1
Indicator for reading first column as row names, specified as the comma-separated pair
consisting of 'ReadRowNames'
and either false
,
true
, 0
, or 1
.
Indicator | Description |
---|---|
| Use when the first column of the region to read contains data, and not the row names for the table. |
| Use when the first column of the region to read contains the row names for the table. |
unspecified | When left unspecified, readtable assumes
false . |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadRowNames
argument in addition to
opts
the import options , then the readtable
behavior
changes based on the specification:
If
ReadRowNames
istrue
, then read the row names from the specified file by using theRowNamesRange
or theRowNameColumn
property of the import options object.If
ReadRowNames
isfalse
, then do not import row names.
Example: 'ReadRowNames',true
Delimiter
— Field delimiter character
character vector | cell array of character vectors | string
Field delimiter character, specified as the comma-separated pair consisting of
'Delimiter'
and a character vector, a cell array of character vectors, or
a string. Specify Delimiter
using any valid character such as a comma
','
or a period '.'
.
This table lists some commonly used field delimiter characters.
Specifier | Field Delimiter |
---|---|
| Comma |
| Space |
| Tab |
| Semicolon |
| Vertical bar |
unspecified | If unspecified, |
To treat consecutive delimiters as a single delimiter, specify
Delimiter
as a cell array of character vectors. In addition, you must
also specify the MultipleDelimsAsOne
option.
Example: 'Delimiter',','
or
'Delimiter','comma'
Data Types: char
| string
| cell
LeadingDelimitersRule
— Procedure to manage leading delimiters
'keep'
| 'ignore'
| 'error'
Procedure to manage leading delimiters in a delimited text file, specified as one of the values in this table.
Leading Delimiters Rule | Behavior |
---|---|
'keep' | Keep the delimiter. |
'ignore' | Ignore the delimiter. |
'error' | Return an error and abort the import operation. |
Example: 'LeadingDelimitersRule','keep'
TrailingDelimitersRule
— Procedure to manage trailing delimiters
'keep'
| 'ignore'
| 'error'
Procedure to manage trailing delimiters in a delimited text file, specified as one of the values in this table.
Leading Delimiters Rule | Behavior |
---|---|
'keep' | Keep the delimiter. |
'ignore' | Ignore the delimiter. |
'error' | Return an error and abort the import operation. |
Example: 'TrailingDelimitersRule','keep'
ConsecutiveDelimitersRule
— Procedure to handle consecutive delimiters
'split'
| 'join'
| 'error'
Procedure to handle consecutive delimiters in a delimited text file, specified as one of the values in this table.
Consecutive Delimiters Rule | Behavior |
---|---|
'split' | Split the consecutive delimiters into multiple fields. |
'join' | Join the delimiters into one delimiter. |
'error' | Return an error and abort the import operation. |
Example: 'ConsecutiveDelimitersRule','split'
Data Types: char
| string
MultipleDelimsAsOne
— Multiple delimiter handling
0 (false)
(default) | 1 (true)
Multiple delimiter handling, specified as the comma-separated
pair consisting of 'MultipleDelimsAsOne'
and either true
or false
.
If true
, then the importing function treats consecutive
delimiters as a single delimiter. Repeated delimiters separated by
white-space are also treated as a single delimiter. You must also
specify the Delimiter
option.
Example: 'MultipleDelimsAsOne',1
NumHeaderLines
— Number of header lines
0
| positive integer
Number of header lines to skip at the beginning of the file, specified as the
comma-separated pair consisting of 'NumHeaderLines'
and either
0
or a positive integer. If unspecified, readtable
automatically detects the number of lines to skip.
Example: 'NumHeaderLines',2
Data Types: single
| double
TreatAsMissing
— Placeholder text to treat as empty value
character vector | cell array of character vectors | string | string array
Placeholder text to treat as an empty value, specified as the comma-separated pair
consisting of 'TreatAsMissing'
and a character vector, cell array of
character vectors, string, or string array. Table elements corresponding to these characters
are set to NaN
.
'TreatAsMissing'
only applies to numeric columns in the table, and
readtable
does not accept numeric literals, such as
'-99'
.
Example: 'TreatAsMissing','N/A'
or
'TreatAsMissing',"N/A"
sets N/A
within numeric columns
to NaN
.
Example: 'TreatAsMissing',{'.','NA','N/A'}
or
'TreatAsMissing',[".","NA","N/A"]
sets .
,
NA
and N/A
within numeric columns to
NaN
.
Data Types: char
| string
MissingRule
— Procedure to manage missing data
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to manage missing data, specified as one of the values in this table.
Missing Rule | Behavior |
---|---|
'fill' | Replace missing data with the contents of the The |
'error' | Stop importing and display an error message showing the missing record and field. |
'omitrow' | Omit rows that contain missing data. |
'omitvar' | Omit variables that contain missing data. |
Example: 'MissingRule','omitrow'
Data Types: char
| string
TextType
— Type for imported text data
"string"
| "char"
Type for imported text data, specified as one of these values:
"string"
— Import text data as string arrays."char"
— Import text data as character vectors.
Example: "TextType","char"
DatetimeType
— Type for imported date and time data
'datetime'
(default) | 'text'
| 'exceldatenum'
(spreadsheet files only)
Type for imported date and time data, specified as the comma-separated pair consisting
of 'DatetimeType'
and one of these values: 'datetime'
,
'text'
, or 'exceldatenum'
. The value
'exceldatenum'
is applicable only for spreadsheet files, and is not valid
for text files.
Value | Type for Imported Date and Time Data |
---|---|
'datetime' | MATLAB
For more information, see |
'text' | If
|
'exceldatenum'
| Excel serial date numbers A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see Differences between the 1900 and the 1904 date system in Excel. |
Example: 'DatetimeType','datetime'
Data Types: char
| string
VariableNamingRule
— Flag to preserve variable names
"modify"
(default) | "preserve"
Flag to preserve variable names, specified as either "modify"
or
"preserve"
.
"modify"
— Convert invalid variable names (as determined by theisvarname
function) to valid MATLAB identifiers."preserve"
— Preserve variable names that are not valid MATLAB identifiers such as variable names that include spaces and non-ASCII characters.
Starting in R2019b, variable names and row names can include any characters, including
spaces and non-ASCII characters. Also, they can start with any characters, not just letters.
Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname
function). To preserve these variable names and row names, set the
value of VariableNamingRule
to "preserve"
. Variable
names are not refreshed when the value of VariableNamingRule
is changed
from "modify"
to "preserve"
.
Example: "VariableNamingRule","preserve"
Data Types: char
| string
ImportErrorRule
— Procedure to handle import errors
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to handle import errors, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
'fill' | Replace the data where the error occurred with the contents of the
The |
'error' | Stop importing and display an error message showing the error-causing record and field. |
'omitrow' | Omit rows where errors occur. |
'omitvar' | Omit variables where errors occur. |
Example: 'ImportErrorRule','omitvar'
Data Types: char
| string
WebOptions
— HTTP
or HTTPS
request options
weboptions
object
HTTP
or HTTPS
request options, specified as a
weboptions
object. The weboptions
object determines how to
import data when the specified filename
is an internet URL containing the
protocol type "http://"
or "https://"
.
ReadVariableNames
— Read first row as variable names
true
| false
| 1
| 0
Indicator for reading the first row as variable names, specified as the comma-separated
pair consisting of 'ReadVariableNames'
and either true
,
false
, 1
, or 0
. If unspecified,
readtable
automatically detects the presence of variable
names.
Indicator | Description |
---|---|
| Use when the first row of the region to read contains the variable names for
the table. |
| Use when the first row of the region to read contains data in the table.
|
unspecified | When left unspecified, readtable automatically detects
true or false and proceeds accordingly. |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadVariableNames
argument in addition to
opts
the import options, then the readtable
behavior
changes based on the specification:
If
ReadVariableNames
istrue
, then read the variable names from the specified file by using theVariableNamesRange
or theVariableNamesLine
property of the import options object.If
ReadVariableNames
isfalse
, then read the variable names from theVariableNames
property of the import options object.
Example: 'ReadVariableNames',true
ExpectedNumVariables
— Expected number of variables
positive integer
Expected number of variables, specified as the comma-separated pair consisting of
'ExpectedNumVariables'
and a positive integer. If unspecified, the
importing function automatically detects the number of variables.
Example: 'ExpectedNumVariables',5
Data Types: single
| double
VariableWidths
— Field widths of variables
vector of positive integer values
Field widths of variables in a fixed-width text file, specified as a vector of positive
integer values. Each positive integer in the vector corresponds to the number of characters
in a field that makes up the variable. The VariableWidths
property
contains an entry corresponding to each variable specified in the
VariableNames
property.
Example: 'VariableWidths',[10,7,4,26,7]
Whitespace
— Characters to treat as white space
character vector | string scalar
Characters to treat as white space, specified as a character vector or string scalar containing one or more characters.
Example: 'Whitespace',' _'
Example: 'Whitespace','?!.,'
EmptyLineRule
— Procedure to handle empty lines
'skip'
| 'read'
| 'error'
Procedure to handle empty lines in the data, specified as 'skip'
,
'read'
, or 'error'
. The importing function interprets
white space as empty.
Empty Line Rule | Behavior |
---|---|
'skip' | Skip the empty lines. |
'read' | Import the empty lines. The importing function parses the empty line using the
values specified in VariableWidths ,
VariableOptions , MissingRule , and other relevant
properties, such as Whitespace . |
'error' | Display an error message and abort the import operation. |
Example: 'EmptyLineRule','skip'
Data Types: char
| string
VariableNamesLine
— Variable names location
0
(default) | positive scalar integer
Variable names location, specified as a positive scalar integer. The
VariableNamesLine
property specifies the line number where variable names
are located.
If VariableNamesLine
is specified as 0, then do not import the
variable names. Otherwise, import the variable names from the specified line.
Example: 'VariableNamesLine',6
Data Types: single
| double
| uint8
| uint16
| uint32
| uint64
PartialFieldRule
— Procedure to handle partial fields
'keep'
| 'fill'
| 'omitrow'
| 'omitvar'
| 'wrap'
| 'error'
Procedure to handle partial fields in the data, specified as one of the values in this table.
Partial Field Rule | Behavior |
---|---|
'keep' | Keep the partial field data and convert the text to the appropriate data type. In some cases, when the importing function is unable to interpret the partial data, a conversion error might occur. |
'fill' | Replace missing data with the contents of the The |
'omitrow' | Omit rows that contain partial data. |
'omitvar' | Omit variables that contain partial data. |
'wrap' | Begin reading the next line of characters. |
'error' | Display an error message and abort the import operation. |
Example: 'PartialFieldRule','keep'
Data Types: char
| string
VariableUnitsLine
— Variable units location
0
(default) | positive scalar integer
Variable units location, specified as a positive scalar integer. The
VariableUnitsLine
property specifies the line number where variable units
are located.
If VariableUnitsLine
is specified as 0, then do not import the
variable units. Otherwise, import the variable units from the specified line.
Example: 'VariableUnitsLine',8
Data Types: single
| double
| uint8
| uint16
| uint32
| uint64
VariableDescriptionsLine
— Variable description location
0
(default) | positive scalar integer
Variable description location, specified as a positive scalar integer. The
VariableDescriptionsLine
property specifies the line number where
variable descriptions are located.
If VariableDescriptionsLine
is specified as 0, then do not import the
variable descriptions. Otherwise, import the variable descriptions from the specified
line.
Example: 'VariableDescriptionsLine',7
Data Types: single
| double
| uint8
| uint16
| uint32
| uint64
ExtraColumnsRule
— Procedure to handle extra columns
'addvars'
| 'ignore'
| 'wrap'
| 'error'
Procedure to handle extra columns in the data, specified as one of the values in this table.
Extra Columns Rule | Behavior |
---|---|
'addvars' | To import extra columns, create new variables. If there are
NOTE: The extra columns are
imported as text with data type |
'ignore' | Ignore the extra columns of data. |
'wrap' | Wrap the extra columns of data to new records. This action does not change the number of variables. |
'error' | Display an error message and abort the import operation. |
Example: 'ExtraColumnsRule','addvars'
Data Types: char
| string
Format
— Column format
character vector | string | 'auto'
Column format of the file, specified as the comma-separated pair consisting of
'Format'
and a character vector or a string scalar having one or more
conversion specifiers, or 'auto'
. The conversion specifiers are the same
as the specifiers accepted by the textscan
function.
Specifying the format can significantly improve speed for some large files. If you do
not specify a value for Format
, then readtable
uses
%q
to interpret nonnumeric columns. The %q
specifier
reads the text and omits double quotation marks ("
) if appropriate.
If you do not specify the
'Format'
name-value pair, then thereadtable
function behaves as though you have used the results of thedetectImportOptions
function to import the data. For more information on the consequences of this behavior, see Compatibility Considerations.If you specify
'Format','auto'
, then the variables created aredouble
arrays, cell array of character vectors, ordatetime
arrays, depending on the data. If the entire column is numeric, variables are imported asdouble
. If any element in a column is not numeric, the variables are imported as cell arrays of character vectors, or asdatetime
arrays if the values represent dates and times.
Example: 'Format','auto'
Data Types: char
| string
EmptyValue
— Returned value for empty numeric fields
NaN
(default) | numeric scalar
Returned value for empty numeric fields in delimited text files, specified as the
comma-separated pair consisting of 'EmptyValue'
and a numeric
scalar.
Example: 'EmptyValue',0
CollectOutput
— Logical indicator determining data concatenation
false
(default) | true
Logical indicator determining data concatenation, specified as the comma-separated pair
consisting of 'CollectOutput'
and either true
or
false
. If true
, then the importing function
concatenates consecutive output cells of the same fundamental MATLAB class into a single array.
Example: 'CollectOutput',true
CommentStyle
— Symbols designating text to ignore
character vector | cell array of character vectors | string | string array
Symbols designating text to ignore, specified as the comma-separated
pair consisting of 'CommentStyle'
and a character
vector, cell array of character vectors, string, or string array.
For example, specify a character such as '%'
to ignore text following the
symbol on the same line. Specify a cell array of two character vectors, such as
{'/*','*/'}
, to ignore any text between those sequences.
MATLAB checks for comments only at the start of each field, not within a field.
Example: 'CommentStyle',{'/*','*/'}
Data Types: char
| string
ExponentCharacter
— Exponent characters
'eEdD'
(default) | character vector | string
Exponent characters, specified as the comma-separated pair consisting of
"ExponentCharacter"
and a character vector or string. The default
exponent characters are e
, E
, d
, and
D
.
Example: "ExponentCharacter","eE"
Data Types: char
| string
LineEnding
— End-of-line characters
character vector | string
End-of-line characters, specified as the comma-separated pair consisting of
'LineEnding'
and a character vector or string. The character
vector must be '\r\n'
or it must specify a single character. Common
end-of-line characters are a newline character ('\n'
) or a carriage
return ('\r'
). If you specify '\r\n'
, then the
importing function treats any of \r
, \n
, and the
combination of the two (\r\n
) as end-of-line characters.
The default end-of-line sequence is \n
, \r
, or
\r\n
, depending on the contents of your file.
If there are missing values and an end-of-line sequence at the end of the last line in
a file, then the importing function returns empty values for those fields. This ensures
that individual cells in output cell array, C
, are the same
size.
Example: 'LineEnding',':'
Data Types: char
| string
DateLocale
— Locale for reading dates
character vector | string scalar
Locale for reading dates, specified as the comma-separated pair consisting of
'DateLocale'
and a character vector or a string scalar of the
form
, where:xx
_YY
YY
is an uppercase ISO 3166-1 alpha-2 code indicating a country.xx
is a lowercase ISO 639-1 two-letter code indicating a language.
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 |
When using the %D
format specifier to read text as
datetime
values, use DateLocale
to specify the
locale in which the importing function should interpret month and day-of-week names and
abbreviations.
If you specify the DateLocale
argument in addition to
opts
the import options, then the importing function uses the
specified value for the DateLocale
argument, overriding the locale
defined in the import options.
Example: 'DateLocale','ja_JP'
DecimalSeparator
— Characters indicating decimal separator
character vector | string scalar
Characters indicating the decimal separator in numeric variables, specified as a character
vector or string scalar. The importing function uses the characters specified in the
DecimalSeparator
name-value pair to distinguish the integer part
of a number from the decimal part.
When converting to integer data types, numbers with a decimal part are rounded to the nearest integer.
Example: If name-value pair is specified as 'DecimalSeparator',','
, then the
importing function imports the text "3,14159"
as the number
3.14159
.
Data Types: char
| string
ThousandsSeparator
— Characters that indicate thousands grouping
character vector | string scalar
Characters that indicate the thousands grouping in numeric variables, specified as a
character vector or string scalar. The thousands grouping characters act as visual
separators, grouping the number at every three place values. The importing function uses
the characters specified in the ThousandsSeparator
name-value pair to
interpret the numbers being imported.
Example: If name-value pair is specified as
'ThousandsSeparator',','
, then the importing function imports the
text "1,234,000"
as 1234000
.
Data Types: char
| string
TrimNonNumeric
— Remove nonnumeric characters
false
(default) | true
Remove nonnumeric characters from a numeric variable, specified as a logical true
or false
.
Example: If name-value pair is specified as 'TrimNonNumeric',true
, then the
importing function reads '$500/-'
as
500
.
Data Types: logical
Encoding
— Character encoding scheme
'UTF-8'
| 'ISO-8859-1'
| 'windows-1251'
| 'windows-1252'
| ...
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
readtable
function uses automatic character set detection to determine
the encoding when reading the file.
If you specify the 'Encoding'
argument in addition to the import
options, then the readtable
function uses the specified value for
'Encoding'
, overriding the encoding defined in the import options.
Example: 'Encoding','UTF-8'
uses UTF-8 as the encoding.
Example: 'Encoding','system'
uses the system default
encoding.
Data Types: char
| string
DurationType
— Output data type of duration data
'duration'
(default) | 'text'
Output data type of duration data from text files, specified as the comma-separated pair
consisting of 'DurationType'
and either 'duration'
or
'text'
.
Value | Type for Imported Duration Data |
---|---|
'duration' | MATLAB
For more information, see |
'text' | If
|
Example: 'DurationType','text'
Data Types: char
| string
HexType
— Output data type of hexadecimal data
'auto'
(default) | 'text'
| 'int8'
| 'int16'
| ...
Output data type of hexadecimal data, specified as the comma-separated pair consisting of 'HexType'
and one of the values listed in the table.
The input file represents hexadecimal values as text, using either 0x
or
0X
as a prefix and the characters
0
-9
,
a
-f
, and A
-F
as digits. (Uppercase and lowercase letters represent the same digits—for example,
'0xf'
and '0xF'
both represent
15
.)
The importing function converts the hexadecimal values to the data type specified by
the value of 'HexType'
.
Value of | Data Type of Output Table Variables |
---|---|
| data type detected automatically |
| unaltered input text |
| 8-bit integer, signed |
| 16-bit integer, signed |
| 32-bit integer, signed |
| 64-bit integer, signed |
| 8-bit integer, unsigned |
| 16-bit integer, unsigned |
| 32-bit integer, unsigned |
| 64-bit integer, unsigned |
Example: 'HexType','uint16'
converts text representing hexadecimal values (such as '0xFF'
) to unsigned 16-bit integers (such as 255
) in the output table.
Data Types: char
| string
BinaryType
— Output data type of binary data
'auto'
(default) | 'text'
| 'int8'
| 'int16'
| ...
Output data type of binary data, specified as the comma-separated pair consisting of
'BinaryType'
and one of the values listed in the table.
The input file represents binary values as text, using either 0b
or
0B
as a prefix and the characters 0
and
1
as digits.
The importing function converts the binary values to the data type specified by the
value of 'BinaryType'
.
Value of | Data Type of Output Table Variables |
---|---|
| data type detected automatically |
| unaltered input text |
| 8-bit integer, signed |
| 16-bit integer, signed |
| 32-bit integer, signed |
| 64-bit integer, signed |
| 8-bit integer, unsigned |
| 16-bit integer, unsigned |
| 32-bit integer, unsigned |
| 64-bit integer, unsigned |
Example: 'BinaryType','uint16'
converts text representing binary
values (such as '0b11111111'
) to unsigned 16-bit integers (such as
255
) in the output table.
Data Types: char
| string
FileType
— Type of file
'spreadsheet'
| 'text'
| 'delimitedtext'
| 'fixedwidth'
| 'xml'
| 'worddocument'
| 'html'
Type of file, specified as the comma-separated pair consisting of
'FileType'
and one of these values.
Value | Import Options |
---|---|
'spreadsheet' | |
'text' |
|
'delimitedtext' |
|
'fixedwidth' |
|
'xml' |
|
'worddocument' |
|
'html' |
|
Use the 'FileType'
name-value pair argument when
filename
does not include the file extension, or when the extension is
not one of these:
.txt
,.dat
, or.csv
for text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
, for XML files.docx
for Microsoft Word document files.html
,.xhtml
, or.htm
for HTML files
Example: 'FileType','text'
Data Types: char
| string
ReadRowNames
— Indicator for reading the first column as row names
false
(default) | true
| 0
| 1
Indicator for reading first column as row names, specified as the comma-separated pair
consisting of 'ReadRowNames'
and either false
,
true
, 0
, or 1
.
Indicator | Description |
---|---|
| Use when the first column of the region to read contains data, and not the row names for the table. |
| Use when the first column of the region to read contains the row names for the table. |
unspecified | When left unspecified, readtable assumes
false . |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadRowNames
argument in addition to
opts
the import options , then the readtable
behavior
changes based on the specification:
If
ReadRowNames
istrue
, then read the row names from the specified file by using theRowNamesRange
or theRowNameColumn
property of the import options object.If
ReadRowNames
isfalse
, then do not import row names.
Example: 'ReadRowNames',true
TreatAsMissing
— Placeholder text to treat as empty value
character vector | cell array of character vectors | string | string array
Placeholder text to treat as an empty value, specified as the comma-separated pair
consisting of 'TreatAsMissing'
and a character vector, cell array of
character vectors, string, or string array. Table elements corresponding to these characters
are set to NaN
.
'TreatAsMissing'
only applies to numeric columns in the table, and
readtable
does not accept numeric literals, such as
'-99'
.
Example: 'TreatAsMissing','N/A'
or
'TreatAsMissing',"N/A"
sets N/A
within numeric columns
to NaN
.
Example: 'TreatAsMissing',{'.','NA','N/A'}
or
'TreatAsMissing',[".","NA","N/A"]
sets .
,
NA
and N/A
within numeric columns to
NaN
.
Data Types: char
| string
MissingRule
— Procedure to manage missing data
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to manage missing data, specified as one of the values in this table.
Missing Rule | Behavior |
---|---|
'fill' | Replace missing data with the contents of the The |
'error' | Stop importing and display an error message showing the missing record and field. |
'omitrow' | Omit rows that contain missing data. |
'omitvar' | Omit variables that contain missing data. |
Example: 'MissingRule','omitrow'
Data Types: char
| string
Range
— Portion of worksheet to read
character vector | string scalar
Portion of the worksheet to read, indicated as a rectangular area specified by a comma
separated pair consisting of 'Range'
and a character vector or string
scalar in one of the following forms.
Ways to specify Range
| Description |
---|---|
Starting Cell | Specify the starting cell for the data as a character vector or string scalar or a two element numeric vector.
Using the starting cell, the importing function automatically detects the extent of the data by beginning the import at the start cell and ending at the last empty row or footer range. Example:
|
Rectangular Range | Specify the range using the syntax
Example:
|
Unspecified or Empty | If unspecified, Example:
Note:
Used Range refers to the rectangular portion of the spreadsheet
that actually contains data. |
Row Range | You can identify range by specifying the beginning and ending rows using
Excel row designators. Then Example:
|
Column Range | You can identify range by specifying the beginning and ending columns using
Excel column designators. Then Example:
|
Named Range in Excel | In Excel, you can create names to identify ranges in the spreadsheet. For instance,
you can select a rectangular portion of the spreadsheet and call it
Example:
|
Example: 'Range'
, 'A1:F10'
Example: 'Range'
, "A1:F10"
—
Data Types: char
| string
DataRange
— Location of data
character vector | string scalar | positive scalar integer | array of positive scalar integers | cell array of character vector | string array
Location of data to be imported, specified as a character vector, string scalar, cell
array of character vectors, string array, positive scalar integer or an
N
-by-2
array of positive scalar integers. Specify
DataRange
using one of these forms.
Specified by | Behavior |
---|---|
Starting Cell or Starting Row | Specify the starting cell for the data, using Excel
Using the starting cell, the importing function automatically detects the extent of the data, by beginning the import at the start cell and ending at the last empty row or footer range. Alternatively, specify the first row containing the data using the positive scalar row index. Using the specified row index, the importing function automatically detects the extent of the data by reading from the specified first row to the end of the data or the footer range. Example:
|
Rectangular Range | Specify the exact range to read using the rectangular range form, where
The importing function only reads the data contained in the specified range. Any empty fields within the specified range are imported as missing cells. The number of columns must
match the number specified in the Example:
|
Row Range or Column Range | Specify the range by identifying the beginning and ending rows using Excel row numbers. Using the specified row range, the importing function automatically detects the column extent by reading from the first nonempty column to the end of the data, and creates one variable per column. Example:
Alternatively, specify the range by identifying the beginning and ending columns using Excel column letters or numbers. Using the specified column range, the import function automatically detects the row extent by reading from the first nonempty row to the end of the data or the footer range. The number of
columns in the specified range must match the number specified in the
Example:
|
Multiple Row Ranges | Specify multiple row ranges to read with an
A valid array of multiple row ranges must:
Use of Example:
|
Unspecified or Empty | Do not fetch any data. Example:
|
Example: 'DataRange'
, 'B2:H15'
Data Types: char
| string
| cell
| single
| double
RowNamesRange
— Location of row names
character vector | string scalar | positive scalar integer | ''
empty character array
Location of row names, specified as a character vector, string scalar, positive scalar
integer, or an empty character array. Specify RowNamesRange
as one of the
values in this table.
Specified by | Behavior |
---|---|
| Specify the starting cell for the data, using Excel
The importing function identifies a name for each variable in the data. Example:
|
Rectangular Range | Specify the exact range to read using the rectangular range form, where
The number of rows
contained in Example:
|
Row Range | Specify range by identifying the beginning and ending rows using Excel row numbers. Row names must be in a single column. Example:
|
Number Index | Specify the column containing the row names using a positive scalar column index. Example:
|
Unspecified or Empty | Indicate that there are no row names. Example:
|
Example: 'RowNamesRange'
, 'A1:H1'
Data Types: char
| single
| double
VariableNamesRange
— Location of variable names
character vector | string scalar | positive scalar integer | ''
empty character array
Location of variable names, specified as a character vector, string scalar, positive
scalar integer, or an empty character array. Specify VariableNamesRange
as
one of the values in this table.
Specified by | Behavior |
---|---|
| Specify the starting cell for the data, using Excel
The importing function reads a name for each variable in the data. Example:
|
Rectangular Range | Specify the exact range to read using the rectangular range form, where
The number of columns must
match the number specified in the Example:
|
Row Range | Specify range by identifying the beginning and ending rows using Excel row numbers. Must be a single row. Example:
|
Number Index | Specify the row containing the variable names using a positive scalar row index. Example:
|
Unspecified or Empty | Indicate that there are no variable names. Example:
|
Example: 'VariableNamesRange'
,
'A1:A15'
Data Types: char
| single
| double
VariableUnitsRange
— Location of variable units
character vector | string scalar | positive scalar integer | ''
empty character array
Location of variable units, specified as a character vector, string scalar, positive
scalar integer, or an empty character array. Specify VariableUnitsRange
as
one of the values in this table.
Specified by | Behavior |
---|---|
| Specify the starting cell for the data, using Excel
The importing function reads a unit for each variable in the data. Example:
|
Rectangular Range | Specify the exact range to read using the rectangular range form, where
The number of columns must
match the number specified in the Example:
|
Row Range | Specify range by identifying the beginning and ending rows using Excel row numbers. Must be a single row. Example:
|
Number Index | Specify the row containing the data units using a positive scalar row index. Example:
|
Unspecified or Empty | Indicate that there are no variable units. Example:
|
Example: 'VariableUnitsRange'
,
'A1:A5'
Data Types: char
| string
| single
| double
VariableDescriptionsRange
— Location of variable descriptions
character vector | string scalar | ''
empty character array
Location of variable descriptions, specified as a character vector, string scalar,
positive scalar integer, or an empty character array. Specify
VariableDescriptionRange
as one of the values in this table.
Specified by | Behavior |
---|---|
| Specify the starting cell for the data, using Excel
The importing function reads a description for each variable in the data. Example:
|
Rectangular Range | Specify the exact range to read using the rectangular range form, where
The number of columns must
match the number specified in the Example:
|
Row Range | Specify range by identifying the beginning and ending rows using Excel row numbers. Must be a single row. Example:
|
Number Index | Specify the row containing the descriptions using a positive scalar row index. Example:
|
Unspecified or Empty | Indicate that there are no variable descriptions. Example:
|
Example: 'VariableDescriptionsRange'
,
'B1:B15'
Data Types: char
| string
| single
| double
TextType
— Type for imported text data
"string"
| "char"
Type for imported text data, specified as one of these values:
"string"
— Import text data as string arrays."char"
— Import text data as character vectors.
Example: "TextType","char"
DatetimeType
— Type for imported date and time data
'datetime'
(default) | 'text'
| 'exceldatenum'
(spreadsheet files only)
Type for imported date and time data, specified as the comma-separated pair consisting
of 'DatetimeType'
and one of these values: 'datetime'
,
'text'
, or 'exceldatenum'
. The value
'exceldatenum'
is applicable only for spreadsheet files, and is not valid
for text files.
Value | Type for Imported Date and Time Data |
---|---|
'datetime' | MATLAB
For more information, see |
'text' | If
|
'exceldatenum'
| Excel serial date numbers A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see Differences between the 1900 and the 1904 date system in Excel. |
Example: 'DatetimeType','datetime'
Data Types: char
| string
VariableNamingRule
— Flag to preserve variable names
"modify"
(default) | "preserve"
Flag to preserve variable names, specified as either "modify"
or
"preserve"
.
"modify"
— Convert invalid variable names (as determined by theisvarname
function) to valid MATLAB identifiers."preserve"
— Preserve variable names that are not valid MATLAB identifiers such as variable names that include spaces and non-ASCII characters.
Starting in R2019b, variable names and row names can include any characters, including
spaces and non-ASCII characters. Also, they can start with any characters, not just letters.
Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname
function). To preserve these variable names and row names, set the
value of VariableNamingRule
to "preserve"
. Variable
names are not refreshed when the value of VariableNamingRule
is changed
from "modify"
to "preserve"
.
Example: "VariableNamingRule","preserve"
Data Types: char
| string
ImportErrorRule
— Procedure to handle import errors
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to handle import errors, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
'fill' | Replace the data where the error occurred with the contents of the
The |
'error' | Stop importing and display an error message showing the error-causing record and field. |
'omitrow' | Omit rows where errors occur. |
'omitvar' | Omit variables where errors occur. |
Example: 'ImportErrorRule','omitvar'
Data Types: char
| string
WebOptions
— HTTP
or HTTPS
request options
weboptions
object
HTTP
or HTTPS
request options, specified as a
weboptions
object. The weboptions
object determines how to
import data when the specified filename
is an internet URL containing the
protocol type "http://"
or "https://"
.
ReadVariableNames
— Read first row as variable names
true
| false
| 1
| 0
Indicator for reading the first row as variable names, specified as the comma-separated
pair consisting of 'ReadVariableNames'
and either true
,
false
, 1
, or 0
. If unspecified,
readtable
automatically detects the presence of variable
names.
Indicator | Description |
---|---|
| Use when the first row of the region to read contains the variable names for
the table. |
| Use when the first row of the region to read contains data in the table.
|
unspecified | When left unspecified, readtable automatically detects
true or false and proceeds accordingly. |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadVariableNames
argument in addition to
opts
the import options, then the readtable
behavior
changes based on the specification:
If
ReadVariableNames
istrue
, then read the variable names from the specified file by using theVariableNamesRange
or theVariableNamesLine
property of the import options object.If
ReadVariableNames
isfalse
, then read the variable names from theVariableNames
property of the import options object.
Example: 'ReadVariableNames',true
ExpectedNumVariables
— Expected number of variables
positive integer
Expected number of variables, specified as the comma-separated pair consisting of
'ExpectedNumVariables'
and a positive integer. If unspecified, the
importing function automatically detects the number of variables.
Example: 'ExpectedNumVariables',5
Data Types: single
| double
Sheet
— Worksheet to read
1
(default) | positive integer | character vector | string
Worksheet to read, specified as the comma-separated pair consisting of
'Sheet'
and a positive integer indicating the worksheet index or a
character vector or string containing the worksheet name. 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
.
If you specify the Sheet
argument in addition to
opts
the import options, then the readtable
function
uses the specified value for Sheet
argument, overriding the sheet name
defined in the import options.
Example: 'Sheet'
, 2
Example: 'Sheet'
, 'MySheetName'
Example: 'Sheet'
, "MySheetName"
Data Types: char
| string
| single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
UseExcel
— Flag to start instance of Microsoft Excel for Windows
false
(default) | true
Flag to start an instance of Microsoft
Excel for Windows when reading 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 importing function starts an instance of Microsoft Excel when reading the file.false
— The importing function does not start an instance of Microsoft Excel when reading the file. When operating in this mode, the importing function functionality 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 reading from spreadsheet files on Windows platforms, if you want to start an instance of Microsoft
Excel, then set the 'UseExcel'
parameter to
true
.
Example: 'UseExcel',true
FileType
— Type of file
'spreadsheet'
| 'text'
| 'delimitedtext'
| 'fixedwidth'
| 'xml'
| 'worddocument'
| 'html'
Type of file, specified as the comma-separated pair consisting of
'FileType'
and one of these values.
Value | Import Options |
---|---|
'spreadsheet' | |
'text' |
|
'delimitedtext' |
|
'fixedwidth' |
|
'xml' |
|
'worddocument' |
|
'html' |
|
Use the 'FileType'
name-value pair argument when
filename
does not include the file extension, or when the extension is
not one of these:
.txt
,.dat
, or.csv
for text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
, for XML files.docx
for Microsoft Word document files.html
,.xhtml
, or.htm
for HTML files
Example: 'FileType','text'
Data Types: char
| string
ReadRowNames
— Indicator for reading the first column as row names
false
(default) | true
| 0
| 1
Indicator for reading first column as row names, specified as the comma-separated pair
consisting of 'ReadRowNames'
and either false
,
true
, 0
, or 1
.
Indicator | Description |
---|---|
| Use when the first column of the region to read contains data, and not the row names for the table. |
| Use when the first column of the region to read contains the row names for the table. |
unspecified | When left unspecified, readtable assumes
false . |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadRowNames
argument in addition to
opts
the import options , then the readtable
behavior
changes based on the specification:
If
ReadRowNames
istrue
, then read the row names from the specified file by using theRowNamesRange
or theRowNameColumn
property of the import options object.If
ReadRowNames
isfalse
, then do not import row names.
Example: 'ReadRowNames',true
TreatAsMissing
— Placeholder text to treat as empty value
character vector | cell array of character vectors | string | string array
Placeholder text to treat as an empty value, specified as the comma-separated pair
consisting of 'TreatAsMissing'
and a character vector, cell array of
character vectors, string, or string array. Table elements corresponding to these characters
are set to NaN
.
'TreatAsMissing'
only applies to numeric columns in the table, and
readtable
does not accept numeric literals, such as
'-99'
.
Example: 'TreatAsMissing','N/A'
or
'TreatAsMissing',"N/A"
sets N/A
within numeric columns
to NaN
.
Example: 'TreatAsMissing',{'.','NA','N/A'}
or
'TreatAsMissing',[".","NA","N/A"]
sets .
,
NA
and N/A
within numeric columns to
NaN
.
Data Types: char
| string
MissingRule
— Procedure to manage missing data
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to manage missing data, specified as one of the values in this table.
Missing Rule | Behavior |
---|---|
'fill' | Replace missing data with the contents of the The |
'error' | Stop importing and display an error message showing the missing record and field. |
'omitrow' | Omit rows that contain missing data. |
'omitvar' | Omit variables that contain missing data. |
Example: 'MissingRule','omitrow'
Data Types: char
| string
TextType
— Type for imported text data
"string"
| "char"
Type for imported text data, specified as one of these values:
"string"
— Import text data as string arrays."char"
— Import text data as character vectors.
Example: "TextType","char"
DatetimeType
— Type for imported date and time data
'datetime'
(default) | 'text'
| 'exceldatenum'
(spreadsheet files only)
Type for imported date and time data, specified as the comma-separated pair consisting
of 'DatetimeType'
and one of these values: 'datetime'
,
'text'
, or 'exceldatenum'
. The value
'exceldatenum'
is applicable only for spreadsheet files, and is not valid
for text files.
Value | Type for Imported Date and Time Data |
---|---|
'datetime' | MATLAB
For more information, see |
'text' | If
|
'exceldatenum'
| Excel serial date numbers A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see Differences between the 1900 and the 1904 date system in Excel. |
Example: 'DatetimeType','datetime'
Data Types: char
| string
VariableNamingRule
— Flag to preserve variable names
"modify"
(default) | "preserve"
Flag to preserve variable names, specified as either "modify"
or
"preserve"
.
"modify"
— Convert invalid variable names (as determined by theisvarname
function) to valid MATLAB identifiers."preserve"
— Preserve variable names that are not valid MATLAB identifiers such as variable names that include spaces and non-ASCII characters.
Starting in R2019b, variable names and row names can include any characters, including
spaces and non-ASCII characters. Also, they can start with any characters, not just letters.
Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname
function). To preserve these variable names and row names, set the
value of VariableNamingRule
to "preserve"
. Variable
names are not refreshed when the value of VariableNamingRule
is changed
from "modify"
to "preserve"
.
Example: "VariableNamingRule","preserve"
Data Types: char
| string
ImportErrorRule
— Procedure to handle import errors
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to handle import errors, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
'fill' | Replace the data where the error occurred with the contents of the
The |
'error' | Stop importing and display an error message showing the error-causing record and field. |
'omitrow' | Omit rows where errors occur. |
'omitvar' | Omit variables where errors occur. |
Example: 'ImportErrorRule','omitvar'
Data Types: char
| string
WebOptions
— HTTP
or HTTPS
request options
weboptions
object
HTTP
or HTTPS
request options, specified as a
weboptions
object. The weboptions
object determines how to
import data when the specified filename
is an internet URL containing the
protocol type "http://"
or "https://"
.
AttributeSuffix
— Attribute suffix
'Attribute'
(default) | character vector | string scalar
Attribute suffix, specified as the comma-separated pair consisting of
'AttributeSuffix'
and either a character vector or string scalar.
This argument specifies the suffix the reading function appends to all table variables
that correspond to attributes in the input XML file. If you do not specify
'AttributeSuffix'
, then the reading function defaults to
appending the suffix 'Attribute'
to all variable names corresponding
to attributes in the input XML file.
Example: 'AttributeSuffix','_att'
ImportAttributes
— Import attributes
logical 1
or true
(default) | logical 0
or false
Import attributes, specified as the comma-separated pair consisting of
'ImportAttributes'
and either 1
(true
) or 0
(false
). If you
specify false
, then the reading function will not import the XML
attributes in the input file as variables in the output table.
Example: 'ImportAttributes',false
RowNodeName
— Table row XML node name
character vector | string array
Table row XML node name, specified as the comma-separated pair consisting of
'RowNodeName'
and either a character vector or string scalar.
This argument specifies the XML node name that delineates rows of the output
table.
Example: 'RowNodeName','XMLNodeName'
RowSelector
— Table row XPath expression
character vector | string scalar
Table row XPath expression, specified as a character vector or string scalar that the
reading function uses to select individual rows of the output table. You must specify
RowSelector
as a valid XPath version 1.0 expression.
Example: 'RowSelector','/RootNode/ChildNode'
VariableNodeNames
— Table variable XML node names
cell array of character vectors | string array
Table variable XML node names, specified as the comma-separated pair consisting of
'VariableNodeNames'
and either a cell array of character vectors
or string array. This argument specifies the XML node name that the reading function
uses to identify the XML nodes to read as variables in the output table.
Example: 'VariableNodeNames',{'XMLNodeName1','XMLNodeName2'}
Example: 'VariableNodeNames',"XMLNodeName"
Example: 'VariableNodeNames',["XMLNodeName1","XMLNodeName2"]
VariableSelectors
— Table variable XPath expressions
cell array of character vectors | string array
Table variable XPath expressions, specified as a cell array of character vectors or
string array that the reading function uses to select table variables. You must specify
VariableSelectors
as valid XPath version 1.0 expressions.
Example: 'VariableSelectors',{'/RootNode/ChildNode'}
Example: 'VariableSelectors',"/RootNode/ChildNode"
Example: 'VariableSelectors',["/RootNode/ChildNode1","/RootNode/ChildNode2"]
TableNodeName
— Table XML node name
character vector | string scalar
Table XML node name, specified as the comma-separated pair consisting of
'TableNodeName'
and either a character vector or string scalar.
This argument specifies the node in the input structure that the reading function should
read to a table.
Example: 'TableNodeName','NodeName'
VariableUnitsSelector
— Variable units XPath expression
character vector | string scalar
Variable units XPath, specified as a character vector or string scalar that the
reading function uses to select the table variable units. You must specify
VariableUnitsSelector
as a valid XPath version 1.0
expression.
Example: 'VariableUnitsSelector','/RootNode/ChildNode'
VariableDescriptionsSelector
— Variable descriptions XPath expression
character vector | string scalar
Variable descriptions XPath expression, specified as a character vector or string
scalar that the reading function reads uses to select the table variable descriptions.
You must specify VariableDescriptionsSelector
as a valid XPath
version 1.0 expression.
Example: 'VariableDescriptionsSelector','/RootNode/ChildNode'
RowNamesSelector
— Table row names XPath expression
character vector | string scalar
Table row names XPath expression, specified as a character vector or string scalar
that the reading function uses to select the names of the table rows. You must specify
RowNamesSelector
as a valid XPath version 1.0 expression.
Example: 'RowNamesSelector','/RootNode/ChildNode'
RepeatedNodeRule
— Procedure to handle repeated XML nodes
'addcol'
(default) | 'ignore'
| 'error'
Procedure to handle repeated XML nodes in a given row of a table, specified as
'addcol'
, 'ignore'
, or
'error'
.
Repeated Node Rule | Behavior |
---|---|
'addcol' | Add columns for the repeated nodes under the variable header in
the table. Specifying the value of
|
'ignore' | Skip importing the repeated nodes. |
'error' | Display an error message and abort the import operation. |
Example: 'RepeatedNodeRule','ignore'
RegisteredNamespaces
— Set of registered XML namespace prefixes
string array
Set of registered XML namespace prefixes, specified as the comma-separated pair
consisting of RegisteredNamespaces
and an array of prefixes. The
reading function uses these prefixes when evaluating XPath expressions on an XML file.
Specify the namespace prefixes and their associated URLs as an Nx2 string array.
RegisteredNamespaces
can be used when you also evaluate an XPath
expression specified by a selector name-value argument, such as
StructSelector
for readstruct
, or
VariableSelectors
for readtable
and
readtimetable
.
By default, the reading function automatically detects namespace prefixes to register
for use in XPath evaluation, but you can also register new namespace prefixes using the
RegisteredNamespaces
name-value argument. You might register a
new namespace prefix when an XML node has a namespace URL, but no declared namespace
prefix in the XML file.
For example, evaluate an XPath expression on an XML file called
example.xml
that does not contain a namespace prefix. Specify
'RegisteredNamespaces'
as ["myprefix",
"https://www.mathworks.com"]
to assign the prefix
myprefix
to the URL
https://www.mathworks.com
.
T = readtable("example.xml", "VariableSelector", "/myprefix:Data",... "RegisteredNamespaces", ["myprefix", "https://www.mathworks.com"])
Example: 'RegisteredNamespaces',["myprefix",
"https://www.mathworks.com"]
TableSelector
— Table data XPath expression
character vector | string scalar
Table data XPath expression, specified as a character vector or string scalar that the
reading function uses to select the output table data. You must specify
TableSelector
as a valid XPath version 1.0 expression.
Example: 'TableSelector','/RootNode/ChildNode'
FileType
— Type of file
'spreadsheet'
| 'text'
| 'delimitedtext'
| 'fixedwidth'
| 'xml'
| 'worddocument'
| 'html'
Type of file, specified as the comma-separated pair consisting of
'FileType'
and one of these values.
Value | Import Options |
---|---|
'spreadsheet' | |
'text' |
|
'delimitedtext' |
|
'fixedwidth' |
|
'xml' |
|
'worddocument' |
|
'html' |
|
Use the 'FileType'
name-value pair argument when
filename
does not include the file extension, or when the extension is
not one of these:
.txt
,.dat
, or.csv
for text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
, for XML files.docx
for Microsoft Word document files.html
,.xhtml
, or.htm
for HTML files
Example: 'FileType','text'
Data Types: char
| string
ReadRowNames
— Indicator for reading the first column as row names
false
(default) | true
| 0
| 1
Indicator for reading first column as row names, specified as the comma-separated pair
consisting of 'ReadRowNames'
and either false
,
true
, 0
, or 1
.
Indicator | Description |
---|---|
| Use when the first column of the region to read contains data, and not the row names for the table. |
| Use when the first column of the region to read contains the row names for the table. |
unspecified | When left unspecified, readtable assumes
false . |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadRowNames
argument in addition to
opts
the import options , then the readtable
behavior
changes based on the specification:
If
ReadRowNames
istrue
, then read the row names from the specified file by using theRowNamesRange
or theRowNameColumn
property of the import options object.If
ReadRowNames
isfalse
, then do not import row names.
Example: 'ReadRowNames',true
TreatAsMissing
— Placeholder text to treat as empty value
character vector | cell array of character vectors | string | string array
Placeholder text to treat as an empty value, specified as the comma-separated pair
consisting of 'TreatAsMissing'
and a character vector, cell array of
character vectors, string, or string array. Table elements corresponding to these characters
are set to NaN
.
'TreatAsMissing'
only applies to numeric columns in the table, and
readtable
does not accept numeric literals, such as
'-99'
.
Example: 'TreatAsMissing','N/A'
or
'TreatAsMissing',"N/A"
sets N/A
within numeric columns
to NaN
.
Example: 'TreatAsMissing',{'.','NA','N/A'}
or
'TreatAsMissing',[".","NA","N/A"]
sets .
,
NA
and N/A
within numeric columns to
NaN
.
Data Types: char
| string
MissingRule
— Procedure to manage missing data
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to manage missing data, specified as one of the values in this table.
Missing Rule | Behavior |
---|---|
'fill' | Replace missing data with the contents of the The |
'error' | Stop importing and display an error message showing the missing record and field. |
'omitrow' | Omit rows that contain missing data. |
'omitvar' | Omit variables that contain missing data. |
Example: 'MissingRule','omitrow'
Data Types: char
| string
EmptyRowRule
— Rule to apply to empty rows in the table
"skip"
(default) | "read"
| "error"
Rule to apply to empty rows in the table, specified as one of the following:
"skip"
– Skip empty rows."read"
– Read empty rows."error"
– Ignore empty rows during table detection and error when reading.
Example: "EmptyRowRule","read"
EmptyColumnRule
— Rule to apply to empty columns in the table
"skip"
(default) | "read"
| "error"
Rule to apply to empty columns in the table, specified as one of the following:
"skip"
– Skip empty columns."read"
– Read empty columns."error"
– Ignore empty columns during table detection and error when reading.
Example: "EmptyColumnRule","error"
TextType
— Type for imported text data
"string"
| "char"
Type for imported text data, specified as one of these values:
"string"
— Import text data as string arrays."char"
— Import text data as character vectors.
Example: "TextType","char"
DatetimeType
— Type for imported date and time data
'datetime'
(default) | 'text'
| 'exceldatenum'
(spreadsheet files only)
Type for imported date and time data, specified as the comma-separated pair consisting
of 'DatetimeType'
and one of these values: 'datetime'
,
'text'
, or 'exceldatenum'
. The value
'exceldatenum'
is applicable only for spreadsheet files, and is not valid
for text files.
Value | Type for Imported Date and Time Data |
---|---|
'datetime' | MATLAB
For more information, see |
'text' | If
|
'exceldatenum'
| Excel serial date numbers A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see Differences between the 1900 and the 1904 date system in Excel. |
Example: 'DatetimeType','datetime'
Data Types: char
| string
VariableNamingRule
— Flag to preserve variable names
"modify"
(default) | "preserve"
Flag to preserve variable names, specified as either "modify"
or
"preserve"
.
"modify"
— Convert invalid variable names (as determined by theisvarname
function) to valid MATLAB identifiers."preserve"
— Preserve variable names that are not valid MATLAB identifiers such as variable names that include spaces and non-ASCII characters.
Starting in R2019b, variable names and row names can include any characters, including
spaces and non-ASCII characters. Also, they can start with any characters, not just letters.
Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname
function). To preserve these variable names and row names, set the
value of VariableNamingRule
to "preserve"
. Variable
names are not refreshed when the value of VariableNamingRule
is changed
from "modify"
to "preserve"
.
Example: "VariableNamingRule","preserve"
Data Types: char
| string
ImportErrorRule
— Procedure to handle import errors
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to handle import errors, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
'fill' | Replace the data where the error occurred with the contents of the
The |
'error' | Stop importing and display an error message showing the error-causing record and field. |
'omitrow' | Omit rows where errors occur. |
'omitvar' | Omit variables where errors occur. |
Example: 'ImportErrorRule','omitvar'
Data Types: char
| string
WebOptions
— HTTP
or HTTPS
request options
weboptions
object
HTTP
or HTTPS
request options, specified as a
weboptions
object. The weboptions
object determines how to
import data when the specified filename
is an internet URL containing the
protocol type "http://"
or "https://"
.
TableIndex
— Index of table to read
1
(default) | positive integer
Index of table to read from Microsoft Word document or HTML file containing multiple tables, specified as a positive integer.
When you specify TableIndex
, the software automatically sets
TableSelector
to the equivalent XPath expression.
Example: 'TableIndex',2
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
MergedCellColumnRule
— Procedure to handle cells with merged columns
"placeleft"
(default) | "placeright"
| "duplicate"
| "omitrow"
| "error"
Procedure to handle cells with merged columns, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
"placeleft" | Place the data in the left-most cell and fill the remaining cells with the
contents of the The
|
"placeright" | Place the data in the right-most cell and fill the remaining cells with the
contents of the The
|
"duplicate" | Duplicate the data in all cells. |
"omitrow" | Omit rows where errors occur. |
"error" | Display an error message and abort the import operation. |
Example: 'MergedCellColumnRule',"placeright"
MergedCellRowRule
— Procedure to handle cells with merged rows
"placeleft"
(default) | "placeright"
| "duplicate"
| "omitrow"
| "error"
Procedure to handle cells with merged rows, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
"placetop" | Place the data in the top cell and fill the remaining cells with the
contents of the The
|
"placebottom" | Place the data in the bottom cell and fill the remaining cells with the
contents of the The
|
"duplicate" | Duplicate the data in all cells. |
"omitvar" | Omit variables where merged rows occur. |
"error" | Display an error message and abort the import operation. |
Example: 'MergedCellRowRule',"duplicate"
VariableNamesRow
— Row containing variable names
nonnegative integer
Row containing variable names, specified as a nonnegative integer.
If you do not specify
VariableNamesRow
, then the software reads variable names according to theReadVariableNames
argument.If
VariableNamesRow
is0
, then the software does not import the variable names.Otherwise, the software imports the variable names from the specified row.
Example: 'VariableNamesRow',2
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
VariableUnitsRow
— Row containing variable units
0
(default) | nonnegative integer
Row containing variable units, specified as a nonnegative integer.
If VariableUnitsRow
is 0
, then the software does
not import the variable units. Otherwise, the software imports the variable units from the
specified row.
Example: 'VariableUnitsRow',3
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
VariableDescriptionsRow
— Row containing variable descriptions
0
(default) | nonnegative integer
Row containing variable descriptions, specified as a nonnegative integer.
If VariableDescriptionsRow
is 0
, then the software
does not import the variable descriptions. Otherwise, the software imports the variable
descriptions from the specified row.
Example: 'VariableDescriptionsRow',4
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
TableSelector
— Table data XPath expression
character vector | string scalar
Table data XPath expression, specified as a character vector or string scalar that the
reading function uses to select the output table data. You must specify
TableSelector
as a valid XPath version 1.0 expression.
Example: 'TableSelector','/RootNode/ChildNode'
FileType
— Type of file
'spreadsheet'
| 'text'
| 'delimitedtext'
| 'fixedwidth'
| 'xml'
| 'worddocument'
| 'html'
Type of file, specified as the comma-separated pair consisting of
'FileType'
and one of these values.
Value | Import Options |
---|---|
'spreadsheet' | |
'text' |
|
'delimitedtext' |
|
'fixedwidth' |
|
'xml' |
|
'worddocument' |
|
'html' |
|
Use the 'FileType'
name-value pair argument when
filename
does not include the file extension, or when the extension is
not one of these:
.txt
,.dat
, or.csv
for text files.xls
,.xlsb
,.xlsm
,.xlsx
,.xltm
,.xltx
, or.ods
for spreadsheet files.xml
, for XML files.docx
for Microsoft Word document files.html
,.xhtml
, or.htm
for HTML files
Example: 'FileType','text'
Data Types: char
| string
ReadRowNames
— Indicator for reading the first column as row names
false
(default) | true
| 0
| 1
Indicator for reading first column as row names, specified as the comma-separated pair
consisting of 'ReadRowNames'
and either false
,
true
, 0
, or 1
.
Indicator | Description |
---|---|
| Use when the first column of the region to read contains data, and not the row names for the table. |
| Use when the first column of the region to read contains the row names for the table. |
unspecified | When left unspecified, readtable assumes
false . |
Note: If both the
'ReadVariableNames'
and 'ReadRowNames'
logical
indicators are true
, then readtable
saves the name in
the first column of the first row of the region to read as the first dimension name in the
property, T.Properties.DimensionNames
.
If you specify the ReadRowNames
argument in addition to
opts
the import options , then the readtable
behavior
changes based on the specification:
If
ReadRowNames
istrue
, then read the row names from the specified file by using theRowNamesRange
or theRowNameColumn
property of the import options object.If
ReadRowNames
isfalse
, then do not import row names.
Example: 'ReadRowNames',true
TreatAsMissing
— Placeholder text to treat as empty value
character vector | cell array of character vectors | string | string array
Placeholder text to treat as an empty value, specified as the comma-separated pair
consisting of 'TreatAsMissing'
and a character vector, cell array of
character vectors, string, or string array. Table elements corresponding to these characters
are set to NaN
.
'TreatAsMissing'
only applies to numeric columns in the table, and
readtable
does not accept numeric literals, such as
'-99'
.
Example: 'TreatAsMissing','N/A'
or
'TreatAsMissing',"N/A"
sets N/A
within numeric columns
to NaN
.
Example: 'TreatAsMissing',{'.','NA','N/A'}
or
'TreatAsMissing',[".","NA","N/A"]
sets .
,
NA
and N/A
within numeric columns to
NaN
.
Data Types: char
| string
MissingRule
— Procedure to manage missing data
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to manage missing data, specified as one of the values in this table.
Missing Rule | Behavior |
---|---|
'fill' | Replace missing data with the contents of the The |
'error' | Stop importing and display an error message showing the missing record and field. |
'omitrow' | Omit rows that contain missing data. |
'omitvar' | Omit variables that contain missing data. |
Example: 'MissingRule','omitrow'
Data Types: char
| string
EmptyRowRule
— Rule to apply to empty rows in the table
"skip"
(default) | "read"
| "error"
Rule to apply to empty rows in the table, specified as one of the following:
"skip"
– Skip empty rows."read"
– Read empty rows."error"
– Ignore empty rows during table detection and error when reading.
Example: "EmptyRowRule","read"
EmptyColumnRule
— Rule to apply to empty columns in the table
"skip"
(default) | "read"
| "error"
Rule to apply to empty columns in the table, specified as one of the following:
"skip"
– Skip empty columns."read"
– Read empty columns."error"
– Ignore empty columns during table detection and error when reading.
Example: "EmptyColumnRule","error"
TextType
— Type for imported text data
"string"
| "char"
Type for imported text data, specified as one of these values:
"string"
— Import text data as string arrays."char"
— Import text data as character vectors.
Example: "TextType","char"
DatetimeType
— Type for imported date and time data
'datetime'
(default) | 'text'
| 'exceldatenum'
(spreadsheet files only)
Type for imported date and time data, specified as the comma-separated pair consisting
of 'DatetimeType'
and one of these values: 'datetime'
,
'text'
, or 'exceldatenum'
. The value
'exceldatenum'
is applicable only for spreadsheet files, and is not valid
for text files.
Value | Type for Imported Date and Time Data |
---|---|
'datetime' | MATLAB
For more information, see |
'text' | If
|
'exceldatenum'
| Excel serial date numbers A serial date number is a single number equal to the number of days from a given reference date. Excel serial date numbers use a different reference date than MATLAB serial date numbers. For more information on Excel dates, see Differences between the 1900 and the 1904 date system in Excel. |
Example: 'DatetimeType','datetime'
Data Types: char
| string
VariableNamingRule
— Flag to preserve variable names
"modify"
(default) | "preserve"
Flag to preserve variable names, specified as either "modify"
or
"preserve"
.
"modify"
— Convert invalid variable names (as determined by theisvarname
function) to valid MATLAB identifiers."preserve"
— Preserve variable names that are not valid MATLAB identifiers such as variable names that include spaces and non-ASCII characters.
Starting in R2019b, variable names and row names can include any characters, including
spaces and non-ASCII characters. Also, they can start with any characters, not just letters.
Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname
function). To preserve these variable names and row names, set the
value of VariableNamingRule
to "preserve"
. Variable
names are not refreshed when the value of VariableNamingRule
is changed
from "modify"
to "preserve"
.
Example: "VariableNamingRule","preserve"
Data Types: char
| string
ImportErrorRule
— Procedure to handle import errors
'fill'
(default) | 'error'
| 'omitrow'
| 'omitvar'
Procedure to handle import errors, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
'fill' | Replace the data where the error occurred with the contents of the
The |
'error' | Stop importing and display an error message showing the error-causing record and field. |
'omitrow' | Omit rows where errors occur. |
'omitvar' | Omit variables where errors occur. |
Example: 'ImportErrorRule','omitvar'
Data Types: char
| string
WebOptions
— HTTP
or HTTPS
request options
weboptions
object
HTTP
or HTTPS
request options, specified as a
weboptions
object. The weboptions
object determines how to
import data when the specified filename
is an internet URL containing the
protocol type "http://"
or "https://"
.
TableIndex
— Index of table to read
1
(default) | positive integer
Index of table to read from Microsoft Word document or HTML file containing multiple tables, specified as a positive integer.
When you specify TableIndex
, the software automatically sets
TableSelector
to the equivalent XPath expression.
Example: 'TableIndex',2
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
MergedCellColumnRule
— Procedure to handle cells with merged columns
"placeleft"
(default) | "placeright"
| "duplicate"
| "omitrow"
| "error"
Procedure to handle cells with merged columns, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
"placeleft" | Place the data in the left-most cell and fill the remaining cells with the
contents of the The
|
"placeright" | Place the data in the right-most cell and fill the remaining cells with the
contents of the The
|
"duplicate" | Duplicate the data in all cells. |
"omitrow" | Omit rows where errors occur. |
"error" | Display an error message and abort the import operation. |
Example: 'MergedCellColumnRule',"placeright"
MergedCellRowRule
— Procedure to handle cells with merged rows
"placeleft"
(default) | "placeright"
| "duplicate"
| "omitrow"
| "error"
Procedure to handle cells with merged rows, specified as one of the values in this table.
Import Error Rule | Behavior |
---|---|
"placetop" | Place the data in the top cell and fill the remaining cells with the
contents of the The
|
"placebottom" | Place the data in the bottom cell and fill the remaining cells with the
contents of the The
|
"duplicate" | Duplicate the data in all cells. |
"omitvar" | Omit variables where merged rows occur. |
"error" | Display an error message and abort the import operation. |
Example: 'MergedCellRowRule',"duplicate"
VariableNamesRow
— Row containing variable names
nonnegative integer
Row containing variable names, specified as a nonnegative integer.
If you do not specify
VariableNamesRow
, then the software reads variable names according to theReadVariableNames
argument.If
VariableNamesRow
is0
, then the software does not import the variable names.Otherwise, the software imports the variable names from the specified row.
Example: 'VariableNamesRow',2
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
VariableUnitsRow
— Row containing variable units
0
(default) | nonnegative integer
Row containing variable units, specified as a nonnegative integer.
If VariableUnitsRow
is 0
, then the software does
not import the variable units. Otherwise, the software imports the variable units from the
specified row.
Example: 'VariableUnitsRow',3
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
VariableDescriptionsRow
— Row containing variable descriptions
0
(default) | nonnegative integer
Row containing variable descriptions, specified as a nonnegative integer.
If VariableDescriptionsRow
is 0
, then the software
does not import the variable descriptions. Otherwise, the software imports the variable
descriptions from the specified row.
Example: 'VariableDescriptionsRow',4
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
TableSelector
— Table data XPath expression
character vector | string scalar
Table data XPath expression, specified as a character vector or string scalar that the
reading function uses to select the output table data. You must specify
TableSelector
as a valid XPath version 1.0 expression.
Example: 'TableSelector','/RootNode/ChildNode'
Output Arguments
T
— Output table
table
Output table, returned as a table. The table can store metadata such as descriptions, variable
units, variable names, and row names. For more information, see the Properties section
of table
.
Tips
Large files in XLSX format sometimes load slowly. For better import and export performance, Microsoft recommends that you use the XLSB format.
Use XPath selectors to specify which elements of the XML input document to import. For example, suppose you want to import the XML file
myFile.xml
, which has the following structure:This table provides the XPath syntaxes that are supported for XPath selector name-value arguments, such as<data> <table category="ones"> <var>1</var> <var>2</var> </table> <table category="tens"> <var>10</var> <var>20</var> </table> </data>
VariableSelectors
orTableSelector
.Selection Operation Syntax Example Result Select every node whose name matches the node you want to select, regardless of its location in the document. Prefix the name with two forward slashes ( //
).data = readtable('myFile.xml', 'VariableSelectors', '//var')
data = 4×1 table var ___ 1 2 10 20
Read the value of an attribute belonging to an element node. Prefix the attribute with an at sign ( @
).data = readtable('myFile.xml', 'VariableSelectors', '//table/@category')
data = 2×1 table categoryAttribute _________________ "ones" "tens"
Select a specific node in a set of nodes. Provide the index of the node you want to select in square brackets ( []
).data = readtable('myFile.xml', 'TableSelector', '//table[1]')
data = 2×1 table var ___ 1 2
Specify precedence of operations. Add parentheses around the expression you want to evaluate first. data = readtable('myFile.xml', 'VariableSelectors', '//table/var[1]')
data = 2×1 table var ___ 1 10
data = readtable('myFile.xml', 'VariableSelectors', '(//table/var)[1]')
data = table var ___ 1
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
Usage notes and limitations:
Text and spreadsheet file workflows are supported in thread-based environments.
Version History
Introduced in R2013bR2023a: Use function in thread-based environments
This function supports thread-based environments for text and spreadsheet file workflows.
R2020a: Results of detectImportOptions
function used by default to import tabular data
By default, the readtable
function uses the results of the
detectImportOptions
function to import tabular data. In essence, these two
function calls behave identically.
T = readtable(filename) T = readtable(filename,detectImportOptions(filename))
In R2020a, there are several differences between the default behavior of
readtable
and its default behavior in previous releases.
Description of Input Fields or Rows | Default R2020a | Default Behavior in Previous Releases |
---|---|---|
First row does not have text to assign as names of output table variables | Assigns the names | Converts the values in the first row of data values to the names of output table variables |
Multiple rows of text as header lines |
|
|
Empty fields | Treat as missing values for detected data type | Treat as empty character vectors or strings |
Values in quotes | Treat as detected data type | Treat as text |
Text that cannot be converted | Treat as missing values for detected data type | Treat as text |
Nonnumeric character trails numeric character without delimiter between them | Treat characters as nonnumeric | Treat numeric and nonnumeric characters as though delimiter separated them |
Input text file has lines with different number of delimiters | Returns output table with extra variables | Raises error message |
To call readtable
with the default behavior it had up to R2019b, use
the 'Format','auto'
name-value pair argument.
T = readtable(filename,'Format','auto')
See Also
Functions
writetable
|table
|textscan
|detectImportOptions
|preview
|readtimetable
|readcell
|readmatrix
|readvars
|setvaropts
Live Editor Tasks
Apps
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)