setvartype
Set variable data types
Description
Examples
Set Data Types for Multiple Variables
Use detectImportOptions
to create import options, set multiple variable data types, and then read the data using readtable
.
Create an options object.
opts = detectImportOptions('patients.xls');
Examine the current (detected) data types of the variables.
disp([opts.VariableNames' opts.VariableTypes'])
{'LastName' } {'char' } {'Gender' } {'char' } {'Age' } {'double' } {'Location' } {'char' } {'Height' } {'double' } {'Weight' } {'double' } {'Smoker' } {'logical'} {'Systolic' } {'double' } {'Diastolic' } {'double' } {'SelfAssessedHealthStatus'} {'char' }
Change the data type of multiple variables depending on your import needs.
opts = setvartype(opts,{'LastName','Gender','Location',... 'Smoker','SelfAssessedHealthStatus'},'string'); opts = setvartype(opts,{'Age','Height','Weight',... 'Systolic','Diastolic'},'single');
Examine the updated data types of the variables.
disp([opts.VariableNames' opts.VariableTypes'])
{'LastName' } {'string'} {'Gender' } {'string'} {'Age' } {'single'} {'Location' } {'string'} {'Height' } {'single'} {'Weight' } {'single'} {'Smoker' } {'string'} {'Systolic' } {'single'} {'Diastolic' } {'single'} {'SelfAssessedHealthStatus'} {'string'}
Import the variables with their updated types using readtable
.
T = readtable('patients.xls',opts);
Input Arguments
opts
— File import options
SpreadsheetImportOptions
| DelimitedTextImportOptions
| FixedWidthImportOptions
File import options, specified as a SpreadsheetImportOptions
, DelimitedTextImportOptions
,
or a FixedWidthImportOptions
object created by
the detectImportOptions
function.
The opts
object contains properties that control
the data import process, such as variable properties, data location
properties, replacement rules, and others.
selection
— Selected variables
character vector | string scalar | cell array of character vector | string array | array of indices | logical array
Selected variables, specified as a character vector, string scalar, cell array of character vectors, string array, array of numeric indices, or a logical array.
Variable names (or indices) must be a subset of the names contained in the
VariableNames
property of the opts
object.
Example: 'Height'
Example: {'Height','LastName'}
Example: [5 9]
Data Types: char
| string
| cell
| uint64
| logical
type
— Variable data type
character vector | string scalar | cell array of character vectors | string array
Variable data type, specified as a character vector, string scalar, cell
array of character vectors, or string array containing valid data type
names. The type
argument designates the data types to use
when importing the variable. Import the variables using one of the data
types listed here.
Data | MATLAB® Data Type |
---|---|
Text |
|
Numeric |
Undefined
floating-point numbers
The same conversion process applies
to all the integer data types:
|
Logical |
|
Date and time |
|
Duration |
|
Categorical |
|
Example: opts = setvartype(opts,'Height','double')
changes the data type of the variable Height
to
double
.
Example: opts =
setvartype(opts,{'Weight','LastName'},{'single','string'})
changes the data type of the variable Weight
to
single
and variable LastName
to
string
.
Data Types: char
| string
| cell
Version History
Introduced in R2016b
See Also
MATLAB Command
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.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)