convertvars
Changes the data type of selected table variables.
Category: Language Fundamentals > Data Types
Syntax
T2 = convertvars(T, vars, dataType)
Converts the chosen variables of table T to dataType.
Description
The conversion applies the function with the name of dataType to each selected variable, for example string(T.Name) or categorical(T.Group), then puts the result back into the table. Unselected variables are not touched.
Input arguments
T- table or timetable.
vars- Variable name, cell array of names, string array, or numeric index.
dataType- Class name such as
'string','categorical','single','int8'.
Output arguments
T2- Table with the same shape, with the chosen variables converted.
Example
Numbers to strings
T = table([1;2], [3;4]);
T2 = convertvars(T, 'Var1', 'string')
class(T2.Var2)
Output
T2 = 2×2 table
Var1 Var2
____ ____
"1" 3
"2" 4
ans = 'double'