Main Content

getvaropts

获取变量导入选项

说明

示例

varOpts = getvaropts(opts,selection) 返回 selection 中指定的变量的变量导入选项。

示例

全部折叠

可以使用 getvaropts 函数访问控制变量导入的变量导入选项对象。

从文件创建导入选项。选项对象是各变量导入选项的集合。

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: [1-by-10 matlab.io.VariableImportOptions] 
	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

获取指定变量的变量导入选项对象。

varOpts = getvaropts(opts, 'Systolic')
varOpts = 
  NumericVariableImportOptions with properties:

   Variable Properties:
                  Name: 'Systolic'
                  Type: 'double'
             FillValue: NaN
        TreatAsMissing: {}
             QuoteRule: 'remove'
              Prefixes: {}
              Suffixes: {}
        EmptyFieldRule: 'missing'

   Numeric Options:
     ExponentCharacter: 'eEdD'
      DecimalSeparator: '.'
    ThousandsSeparator: ''
        TrimNonNumeric: 0
          NumberSystem: 'decimal'

要修改变量导入选项,请参阅 setvaroptssetvartype 函数参考页。

输入参数

全部折叠

文件导入选项,指定为 detectImportOptions 函数创建的 SpreadsheetImportOptionsDelimitedTextImportOptionsFixedWidthImportOptions 对象。opts 对象包含控制数据导入过程的属性,例如变量属性、数据位置属性、替代规则等。

选定的变量,指定为字符向量、字符串标量、字符向量元胞数组、字符串数组、数值索引数组或逻辑数组。

变量的名称(或索引)必须为 opts 对象的 VariableNames 属性中包含的名称的子集。

示例: 'Height'

示例: {'Height','LastName'}

示例: [5 9]

数据类型: char | string | cell | uint64

输出参量

全部折叠

选定变量的类型相关选项,以变量导入选项对象数组形式返回。此数组为 selection 参量中指定的每个变量包含一个对应的对象。根据变量的数据类型,数组中的每个对象可为以下类型之一:数值、文本、logicaldatetimecategorical

您可以修改各个对象的属性。

  • 要修改 Type 属性,请使用 setvartype 函数。

  • 要修改其他属性,请使用 setvaropts 函数。

版本历史记录

在 R2016b 中推出