table

Methods

Method Reference: table.convertvars

table: tblB = convertvars (tblA, vars, dataType)

Convert table variables to specified data type.

tblB = convertvars (tblA, vars, dataType) converts the variables in tblA specified by vars to the specified data type.

vars can be any of the following types.

  • a character vector specifying a single variable.
  • a cell array of character vectors specifying a single or multiple variables.
  • a string array specifying a single or multiple variables.
  • a numeric array of integer values indexing the variables to be converted.
  • a logical vector of the same length as the width of the table tblA indexing as true the variables to be converted.
  • a vartype object used to create a subscript that selects variables of a specified type.

dataType specifies the data type to convert those variables to. It can either be a character vector defining the name of the data type to convert to or a function handle, which will perform the conversion. When specifying a name for data type conversion, it can either be a one-argument constructor for the specified data type, which must accept the selected variables’ current data types as input, or an available method, which can be applied on selected variables’ current data types. When specifying a function handle for applying a conversion on selected variables, this function handle must accept a single input argument and return in its output the same rows as the input argument.

Either way, each resulting variable must have the same number of rows as the respective variable selected for conversion. However, depending on the chosen type of conversion, the columns of the converted variable(s) might differ. It is up to the user to ensure that the appropriate type of conversion is performed. convertvars only checks the custom function handles for returning the correct number of rows, which must equal the number of rows of the input table, tblA.

Source Code: table

Example: 1

convertvars changes the data type of selected variables, leaving the rest alone. The target type is given as a name, or as a conversion function.

 T = table ([1; 0; 1], [38; 43; 40], 'VariableNames', {'Smoker', 'Age'})
T =
  3x2 table

    Smoker    Age    
    ______    ___    

         1     38    
         0     43    
         1     40
 convertvars (T, 'Smoker', 'logical')
ans =
  3x2 table

    Smoker    Age    
    ______    ___    

    true       38    
    false      43    
    true       40

Select several variables at once and hand them a conversion function.

 T2 = table ({'M'; 'F'; 'M'}, {'NY'; 'CA'; 'MA'}, ...
             'VariableNames', {'Gender', 'State'})
T2 =
  3x2 table

    Gender    State     
    ______    ______    

    {'M' }    {'NY'}    
    {'F' }    {'CA'}    
    {'M' }    {'MA'}
 convertvars (T2, {'Gender', 'State'}, 'categorical')
ans =
  3x2 table

    Gender    State    
    ______    _____    

         M       NY    
         F       CA    
         M       MA