Main Content

Define Import Options for Tables

Typically, you can import tables using thereadtable函数。然而,有时进口禁忌lar data requires additional control over the import process. For example, you might want to select the variables to import or handle rows with missing or error-causing data. To control the import process, you can create an import options object. The object has properties that you can adjust based on your import needs.

Create Import Options

To create an import options object for a sample data set,airlinesmall.csv, use thedetectImportOptions函数。ThedetectImportOptionsfunction creates aDelimitedTextImportOptionsobject for this text file. For a full list of properties of the import options object, see thedetectImportOptionsreference page.

opts = detectImportOptions('airlinesmall.csv');

Customize Table-Level Import Options

The import options object has properties that you can adjust to control the import process. Some properties apply to the entire table while others apply to specific variables. Properties that affect the entire table include rules to manage error-causing or missing data. For example, remove rows with data that cause import errors by setting theImportErrorRuleto'omitrow'. Replace missing values by setting theMissingRuleto'fill'. TheFillValueproperty value determines what value replaces the missing values. For example, you can replace missing values withNaN.

opts.ImportErrorRule ='omitrow'; opts.MissingRule ='fill';

Customize Variable-Level Import Options

To get and set options for specific variables use thegetvaropts,setvartype, andsetvaropts功能。例如,查看当前选择for the variables namedFlightNum,起源,Dest, andArrDelay, using thegetvaropts函数。

getvaropts(opts,{'FlightNum','Origin','Dest','ArrDelay'});

Change the data types for the variables using thesetvartypefunction:

  • Since the values in the variableFlightNumare identifiers for the flight and not numerical values, change its data type tochar.

  • Since the variables起源andDestdesignate a finite set of repeating text values, change their data type tocategorical.

opts = setvartype(opts,{'FlightNum','Origin','Dest','ArrDelay'},...{'char','categorical','categorical','single'});

Change other properties using thesetvaroptsfunction:

  • For theFlightNumvariable, remove any leading white spaces from the text by setting theWhiteSpaceRuleproperty totrimleading.

  • For theArrDelayvariable, replace fields containing0orNAwith the value specified inFillValueproperty by setting theTreatAsMissingproperty.

opts = setvaropts(opts,'FlightNum','WhitespaceRule','trimleading'); opts = setvaropts(opts,'ArrDelay','TreatAsMissing',{'0','NA'});

Import Table

Specify the variables to get, import them usingreadtable, and display the first8rows of the table.

opts.SelectedVariableNames = {'FlightNum','Origin','Dest','ArrDelay'}; T = readtable('airlinesmall.csv',opts); T(1:8,:)
ans=8×4 tableFlightNum Origin Dest ArrDelay _________ ______ ____ ________ {'1503'} LAX SJC 8 {'1550'} SJC BUR 8 {'1589'} SAN SMF 21 {'1655'} BUR SJC 13 {'1702'} SMF LAX 4 {'1729'} LAX SJC 59 {'1763'} SAN SFO 3 {'1800'} SEA LAX 11

See Also

|||||||||

Related Topics