convertvars
Convert table or timetable variables to specified data type
Description
T2 = convertvars(
converts the specified variables to the specified data type. The input argument
T1
,vars
,dataType
)T1
can be a table or timetable.
While you can specify dataType
as the name of a data type, you also
can specify it as a function handle. In that case, it is a handle to a function that
converts or otherwise modifies the variables specified by vars
.
Similarly, vars
can contain variable names or positions of variables in
T1
, or it can be a handle to a function that identifies
variables.
Examples
Input Arguments
Alternative Functionality
You can also convert table and timetable variables
by using the VariableTypes property. Unlike
convertvars
, the property enables you to convert different variables to
different data types. (since R2024b)
For example, assign new data types to the variables of table T
so that
the first variable is a categorical array and the last variable is a string array.
T = readtable('outages.csv');
head(T,3)
Region OutageTime Loss Customers RestorationTime Cause _____________ ________________ ______ __________ ________________ ___________________ {'SouthWest'} 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 {'winter storm' } {'SouthEast'} 2003-01-23 00:49 530.14 2.1204e+05 NaT {'winter storm' } {'SouthEast'} 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 {'winter storm' }
T.Properties.VariableTypes = ["categorical" "datetime" "double" "double" "datetime" "string"]; head(T,3)
Region OutageTime Loss Customers RestorationTime Cause _________ ________________ ______ __________ ________________ ______________ SouthWest 2002-02-01 12:18 458.98 1.8202e+06 2002-02-07 16:50 "winter storm" SouthEast 2003-01-23 00:49 530.14 2.1204e+05 NaT "winter storm" SouthEast 2003-02-07 21:15 289.4 1.4294e+05 2003-02-17 08:14 "winter storm"
Extended Capabilities
Version History
Introduced in R2018b
See Also
addvars
| mergevars
| removevars
| splitvars
| movevars
| renamevars