创建导入选项,定制多个变量的数据类型,然后读取数据。
根据文本文件创建导入选项对象。
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
检查变量 TaxiIn 和 TaxiOut 的 Type 属性。
getvaropts(opts,{'TaxiIn','TaxiOut'})
ans =
1x2 TextVariableImportOptions array with properties:
Name
Type
FillValue
TreatAsMissing
QuoteRule
Prefixes
Suffixes
EmptyFieldRule
WhitespaceRule
将变量 TaxiIn 和 TaxiOut 的类型更改为 double。
opts = setvartype(opts,{'TaxiIn','TaxiOut'},'double');
指定要导入和检查的变量的子集。
opts.SelectedVariableNames = {'TaxiIn','TaxiOut'};
使用 readtable 函数以及选项对象导入选定的变量。显示表的摘要。
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