delimitedTextImportOptions
Import options object for delimited text
Description
ADelimitedTextImportOptions
object enables you to specify how MATLAB®imports tabular data from delimited text files. The object contains properties that control the data import process, including the handling of errors and missing data.
Creation
You can create aDelimitedTextImportOptions
object using either thedetectImportOptions
function or thedelimitedTextImportOptions
function (described here):
Use
detectImportOptions
to detect and populate the import properties based on the contents of the delimited text file specified infilename
.opts = detectImportOptions(filename);
Use
delimitedTextImportOptions
to define the import properties based on your import requirements.
Syntax
Description
opts = delimitedTextImportOptions
creates aDelimitedTextImportOptions
object with one variable.
opts = delimitedTextImportOptions('NumVariables',
creates the object with the number of variables specified innumVars
)numVars
.
opts = delimitedTextImportOptions(___,
specifies additionalpropertiesforName,Value
)DelimitedTextImportOptions
object using one or more name-value pair arguments.
Input Arguments
numVars
—Number of variables
positive scalar integer
Number of variables, specified as a positive scalar integer.
Properties
Variable Properties
VariableNames
—Variable names
cell array of character vectors|string array
Variable names, specified as a cell array of character vectors or string array. TheVariableNames
property contains the names to use when importing variables.
If the data containsN
variables, but no variable names are specified, then theVariableNames
property contains{'Var1','Var2',...,'VarN'}
.
To support invalid MATLAB identifiers as variable names, such as variable names containing spaces and non-ASCII characters, set the value ofVariableNamingRule
to'preserve'
.
Example:选择。VariableNames
returns the current (detected) variable names.
Example:选择。VariableNames(3) = {'Height'}
changes the name of the third variable toHeight
.
Data Types:char
|string
|cell
VariableNamingRule
—Flag to preserve variable names
"modify"
(default) |"preserve"
Flag to preserve variable names, specified as either"modify"
or"preserve"
.
"modify"
——转换无效的变量名(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 theisvarname
function). To preserve these variable names and row names, set the value ofVariableNamingRule
to"preserve"
. Variable names are not refreshed when the value ofVariableNamingRule
is changed from"modify"
to"preserve"
.
Data Types:char
|string
VariableTypes
—数据类型的变量
cell array of character vectors|string array
Data type of variable, specified as a cell array of character vectors, or string array containing a set of valid data type names. TheVariableTypes
property designates the data types to use when importing variables.
To update theVariableTypes
property, use thesetvartype
function.
Example:选择。VariableTypes
returns the current variable data types.
Example:opts = setvartype(opts,'Height',{'double'})
changes the data type of the variableHeight
todouble
.
SelectedVariableNames
—Subset of variables to import
character vector|string scalar|cell array of character vectors|string array|array of numeric indices
Subset of variables to import, specified as a character vector, string scalar, cell array of character vectors, string array or an array of numeric indices.
SelectedVariableNames
must be a subset of names contained in theVariableNames
property. By default,SelectedVariableNames
包含所有的变量names from theVariableNames
property, which means that all variables are imported.
Use theSelectedVariableNames
property to import only the variables of interest. Specify a subset of variables using theSelectedVariableNames
property and usereadtable
to import only that subset.
To support invalid MATLAB identifiers as variable names, such as variable names containing spaces and non-ASCII characters, set the value ofVariableNamingRule
to'preserve'
.
Example:选择。SelectedVariableNames = {'Height','LastName'}
selects only two variables,Height
andLastName
, for the import operation.
Example:选择。SelectedVariableNames = [1 5]
selects only two variables, the first variable and the fifth variable, for the import operation.
Example:T = readtable(filename,opts)
returns a table containing only the variables specified in theSelectedVariableNames
property of theopts
object.
Data Types:uint16
|uint32
|uint64
|char
|string
|cell
VariableOptions
—Type specific variable import options
array of variable import options objects
Type specific variable import options, returned as an array of variable import options objects. The array contains an object corresponding to each variable specified in theVariableNames
property. Each object in the array contains properties that support the importing of data with a specific data type.
Variable options support these data types: numeric, text,logical
,datetime
, or分类
.
To query the current (or detected) options for a variable, use thegetvaropts
function.
To set and customize options for a variable, use thesetvaropts
function.
Example:选择。VariableOptions
returns a collection ofVariableImportOptions
objects, one corresponding to each variable in the data.
Example:getvaropts(opts,'Height')
returns theVariableImportOptions
object for theHeight
variable.
Example:opts = setvaropts(opts,'Height','FillValue',0)
sets theFillValue
property for the variableHeight
to0
.
Location Properties
DataLines
—Data location
positive scalar integer|array of positive scalar integers
Data location, specified as a positive scalar integer or aN-
by-2
array of positive scalar integers. SpecifyDataLines
using one of these forms.
Specify as |
Description |
---|---|
|
Specify thefirst linethat contains the data. Specifying the value using
|
|
Specify theline rangethat contains the data. Values in the array |
|
Specifymultiple line rangesto read with an A valid array of multiple line ranges must:
When specifying multiple line ranges, use |
Example:选择。DataLines = 5
sets theDataLines
property to the value[5 inf]
. Read all rows of data starting from row5
to the end-of-file.
Example:选择。DataLines = [2 6]
sets the property to read lines2
through6
.
Example:选择。DataLines = [1 3; 5 6; 8 inf]
sets the property to read rows1
,2
,3
,5
,6
, and all rows between8
, and the end-of-file.
Data Types:single
|double
|uint8
|uint16
|uint32
|uint64
RowNamesColumn
—Row names location
0
(default) |positive scalar integer
Row names location, specified as a positive scalar integer. TheRowNamesColumn
property specifies the location of the column containing the row names.
IfRowNamesColumn
is specified as 0, then do not import the row names. Otherwise, import the row names from the specified column.
Example:选择。RowNamesColumn = 2;
Data Types:single
|double
|uint8
|uint16
|uint32
|uint64
VariableNamesLine
—Variable names location
0
(default) |positive scalar integer
Variable names location, specified as a positive scalar integer. TheVariableNamesLine
属性指定变量的行号names are located.
IfVariableNamesLine
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
VariableDescriptionsLine
—Variable description location
0
(default) |positive scalar integer
Variable description location, specified as a positive scalar integer. TheVariableDescriptionsLine
属性指定变量的行号descriptions are located.
IfVariableDescriptionsLine
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
VariableUnitsLine
—Variable units location
0
(default) |positive scalar integer
Variable units location, specified as a positive scalar integer. TheVariableUnitsLine
属性指定变量的行号units are located.
IfVariableUnitsLine
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
Delimited Text Properties
Delimiter
—Field delimiter characters
character vector|string scalar|cell array of character vectors|string array
Field delimiter characters in a delimited text file, specified as a character vector, string scalar, cell array of character vectors, or string array.
Example:'Delimiter','|'
Example:'Delimiter',{';','*'}
Data Types:char
|string
|cell
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','?!.,'
LineEnding
—End-of-line characters
{'\n','\r','\r\n'}
(default) |character vector|string scalar|cell array of character vectors|string array
End-of-line characters, specified as a character vector, string scalar, cell array of character vectors, or string array.
Example:'LineEnding','\n'
Example:'LineEnding','\r\n'
Example:'LineEnding',{'\b',':'}
Data Types:char
|string
|cell
CommentStyle
—Style of comments
character vector|string scalar|cell array of character vectors|string array
Style of comments, specified as a character vector, string scalar, cell array of character vectors, or string array.
For example, to ignore the text following a percent sign on the same line, specifyCommentStyle
as'%'
.
Example:'CommentStyle',{'/*'}
Data Types:char
|string
|cell
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. |
Data Types:char
|string
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. |
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. |
Encoding
—Character encoding scheme
''
|'UTF-8'
|'system'
|'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 function uses automatic character set detection to determine the encoding when reading the file.
Example:'Encoding','system'
uses the system default encoding.
Data Types:char
|string
Replacement Rules
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
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 inVariableWidths ,VariableOptions ,MissingRule , and other relevant properties, such asWhitespace . |
'error' |
Display an error message and abort the import operation. |
Example:选择。EmptyLineRule = 'skip';
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
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 |
'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. |
Data Types:char
|string
Object Functions
getvaropts |
Get variable import options |
setvaropts |
Set variable import options |
setvartype |
Set variable data types |
preview |
Preview eight rows from file using import options |
Examples
Define Import Options for Variables in Delimited Text File
Define an import options object to read multiple variables frompatients.dat
.
Based on the contents of your file, define these variable properties: names, types, delimiter character, data starting location, and the extra column rule.
varNames = {'LastName','Gender','Age','Location','Height','Weight','Smoker'} ; varTypes = {'char','categorical','int32','char','double','double','logical'} ; delimiter =','; dataStartLine = 2; extraColRule ='ignore';
Use thedelimitedTextImportOptions
function and your variable information to initialize the import options objectopts
.
opts = delimitedTextImportOptions('VariableNames',varNames,...'VariableTypes',varTypes,...'Delimiter',delimiter,...'DataLines', dataStartLine,...'ExtraColumnsRule',extraColRule);
Use thepreview
function with the import options object to preview the data.
preview('patients.dat',opts)
ans=8×7 tableLastName Gender Age Location Height Weight Smoker ____________ ______ ___ _____________________________ ______ ______ ______ {'Smith' } Male 38 {'County General Hospital' } 71 176 false {'Johnson' } Male 43 {'VA Hospital' } 69 163 false {'Williams'} Female 38 {'St. Mary's Medical Center'} 64 131 false {'Jones' } Female 40 {'VA Hospital' } 67 133 false {'Brown' } Female 49 {'County General Hospital' } 64 119 false {'Davis' } Female 46 {'St. Mary's Medical Center'} 68 142 false {'Miller' } Female 33 {'VA Hospital' } 64 142 false {'Wilson' } Male 40 {'VA Hospital' } 68 180 false
Import the data usingreadtable
.
T = readtable('patients.dat',opts); whosT
Name Size Bytes Class Attributes T 100x7 30563 table
Tips
Introduced in:
R2016b —
DelimitedTextImportOptions
objectR2018b —
delimitedTextImportOptions
function
See Also
Abrir ejemplo
Tiene una versión modificada de este ejemplo. ¿Desea abrir este ejemplo con sus modificaciones?
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
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)