MathWorks Machine Translation
The automated translation of this page is provided by a general purpose third party translator tool.
MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.
Import options object for delimited text
A DelimitedTextImportOptions object
enables you to specify how MATLAB® imports tabular data from text
files. The object contains properties that control the data import
process, including the handling of errors and missing data. Use a DelimitedTextImportOptions object
to query the current (detected) values of import properties or to
assign new values based on your import needs.
Create a DelimitedTextImportOptions object
using the detectImportOptions function.
VariableNames — Variable namesVariable names, specified as a cell array of character vectors.
The VariableNames property contains the names to
use when importing variables.
If the data contains N variables, but no
variable names are detected, then the VariableNames property
contains {'Var1','Var2',...,'VarN'}.
Example: opts.VariableNames returns the current
(detected) variable names.
Example: opts.VariableNames(3)
= {'Height'} changes the name of the third variable to Height.
Data Types: char | cell
VariableTypes — Variable data typesVariable data types, specified as a cell array of character
vectors. The VariableTypes property designates
the data types to use when importing variables. When assigning new
values, specify VariableTypes as a cell array of
valid data type names.
To update the VariableTypes property, use
the setvartype function.
Example: opts.VariableTypes returns the current
(detected) variable data types.
Example: opts
= setvartype(opts,'Height',{'double'}) changes the data
type of the variable Height to double.
Data Types: cell | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | categorical | datetime
SelectedVariableNames — Subset of variables to importSubset of variables to import, specified as a character vector, a cell array of character vectors, or an array of numeric indices.
SelectedVariableNames must be a subset of
names contained in the VariableNames property.
By default, SelectedVariableNames contains all
the variable names from the VariableNames property,
which means that all variables are imported.
Use the SelectedVariableNames property to
import only the variables of interest. Specify a subset of variables
using the SelectedVariableNames property and use readtable to import only that subset.
Example: opts.SelectedVariableNames = {'Height','LastName'} selects
only two variables, Height and LastName,
for the import operation.
Example: opts.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 the SelectedVariableNames property
of the opts object.
Data Types: uint16 | uint32 | uint64 | logical | char | cell
VariableOptions — Type specific variable import optionsType specific variable import options, returned as an array
of variable import options objects. The array contains an object corresponding
to each variable specified in the VariableNames 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 categorical.
To query the current (or detected) options for a variable, use
the getvaropts function.
To set and customize options for a variable, use the setvaropts function.
Example: opts.VariableOptions returns a collection
of VariableImportOptions objects, one corresponding
to each variable in the data.
Example: getvaropts(opts,'Height') returns
the VariableImportOptions object for the Height variable.
Example: opts = setvaropts(opts,'Height','FillValue',0) sets
the FillValue property for the variable Height to 0.
DataLine — Data start locationData start location, specified as a positive scalar integer.
The DataLine property specifies the line number
where data begins in the text file.
The value for the DataLine property must
be greater than 0.
Example: opts.DataLine = 5;
Data Types: single | double | uint8 | uint16 | uint32 | uint64
RowNamesColumn — Row names location0 (default) | positive scalar integerRow names location, specified as a positive scalar integer.
The RowNamesColumn property specifies the location
of the column containing the row names.
If RowNamesColumn is specified as 0, then
do not import the row names. Otherwise, import the row names from
the specified column.
Example: opts.RowNamesColumn = 2;
Data Types: single | double | uint8 | uint16 | uint32 | uint64
VariableNamesLine — Variable names location0 (default) | positive scalar integerVariable 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: opts.VariableNamesLine = 6;
Data Types: single | double | uint8 | uint16 | uint32 | uint64
VariableDescriptionsLine — Variable description location0 (default) | positive scalar integerVariable 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: opts.VariableDescriptionsLine = 7;
Data Types: single | double | uint8 | uint16 | uint32 | uint64
VariableUnitsLine — Variable units location0 (default) | positive scalar integerVariable 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: opts.VariableUnitsLine = 8;
Data Types: single | double | uint8 | uint16 | uint32 | uint64
Delimiter — Field delimiter charactersField delimiter characters, specified as a character vector or a cell array of character vectors.
Example: '|'
Example: {';','*'}
Data Types: char | cell
Whitespace — Characters to treat as white spaceCharacters to treat as white space, specified as a character vector of one or more characters.
Example: ' _'
Example: '?!.,'
LineEnding — End-of-line characters{'\n','\r','\r\n'} (default) | character vector | cell array of character vectorsEnd-of-line characters, specified as a character vector of one or more characters or a cell array of character vectors.
Example: '\n'
Example: '\r\n'
Example: {'\b',':'}
Data Types: char | cell
CommentStyle — Style of commentsStyle of comments, specified as a character vector or cell array of character vectors.
For example, specify CommentStyle as '%' to
ignore the text following a percent sign on the same line.
Example: {'/*'}
Data Types: char | cell
ConsecutiveDelimitersRule — Procedure to handle consecutive delimiters'split' | 'join' | 'error'Procedure to handle consecutive delimiters, 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' | Error and abort the import operation. |
Example: 'join'
LeadingDelimitersRule — Procedure to manage leading delimiters'keep' | 'ignore' | 'error'Procedure to manage leading delimiters, specified as one of the values in this table.
| Rule | Behavior |
|---|---|
'keep' | Keep the delimiter. |
'ignore' | Ignore the delimiter. |
'error' | Error and abort the import operation. |
Example: 'ignore'
Encoding — File encoding'system' (default) | 'UTF-8' | 'ISO-8859-1' | 'windows-1251' | 'windows-1252' | ...File encoding, specified as 'system' or a
standard character encoding scheme name like one of the values in
this table.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
Example: 'system' uses the system default
encoding.
MissingRule — Procedure to manage missing data'fill' | '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: opts.MissingRule = 'omitrow';
ImportErrorRule — Procedure to handle import errors'fill' | '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: opts.ImportErrorRule = 'omitvar';
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' | Error and abort the import operation. |
Example:
getvaropts |
Get variable import options |
setvaropts |
Set variable import options |
setvartype |
Set variable data types |
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'
EmptyLineRule: 'skip'
Encoding: 'US-ASCII'
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
Variable Import Properties: Set types by name with setvartype
VariableNames: {1×29 cell}
VariableTypes: {1×29 cell}
SelectedVariableNames: {1×29 cell}
Location Properties:
DataLine: 2
VariableNamesLine: 1
RowNamesColumn: 0
VariableUnitsLine: 0
VariableDescriptionsLine: 0
VariableOptions: [1×29 VariableOptions]
Access VariableOptions sub-properties using setvaropts/getvaropts
Examine the Type property of variables TaxiIn and TaxiOut.
getvaropts(opts,{'TaxiIn','TaxiOut'})
ans =
1×2 TextVariableImportOptions array with properties:
WhitespaceRule
Type
FillValue
Name
QuoteRule
TreatAsMissing
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: 123523×1 double
Values:
min 0
median 5
max 1451
NaNs 37383
TaxiOut: 123523×1 double
Values:
min 0
median 13
max 755
NaNs 37364
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.
You can also select a location from the following list: