This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Set variable data types


opts = setvartype(opts,type)
opts = setvartype(opts,selection,type)


opts = setvartype(opts,type) updates all the variables in the opts object based on the specified type. type must be a character vector or a cell array of character vector of valid data type names.


opts = setvartype(opts,selection,type) updates data type for only the variables specified in the selection argument.


collapse all

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',...
opts = setvartype(opts,{'Age','Height','Weight',...

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

collapse all

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.

Selected variables, specified as a character vector, cell array of character vectors, or an array of numeric indices.

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 | cell | uint64

Variable data type, specified as a character vector or a cell array of character vectors 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.

DataMATLAB® Data Type

'char' or 'string'


'single', 'double', 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', or 'uint64'

Undefined floating-point numbers NaN, -Inf, +Inf are only valid for single and double data types. Therefore, when you change the type of floating-point data to an integer, the importing function converts the undefined floating-point numbers. For example, when converting to the 'uint8' data type:

  • NaN is converted to 0.

  • -Inf is converted to intmin('int8').

  • +Inf is converted to intmax('int8').

The same conversion process applies to all the integer data types: int8, int16, int16, int32, int64, uint8, uint16, uint32, or uint64.



Date and time




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: cell | single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | categorical | datetime

Introduced in R2016b

Was this topic helpful?