table

Methods

Method Reference: table.removevars

table: tblB = removevars (tblA, vars)

Remove variables from a table.

tblB = removevars (tblA, vars) removes the variables specified by vars from the input table tblA.

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 removed.
  • a logical vector of the same length as the width of the table tblA indexing as true the variables to be removed.
  • a vartype object used to create a subscript that selects variables of a specified type.

Source Code: table

Example: 1

removevars drops variables, selected by name or by position index.

 Name = string ({'Sanchez'; 'Johnson'; 'Li'});
 Age = [38; 43; 40];
 Gender = categorical ({'M'; 'M'; 'F'});
 Visit = datetime (2024, [1; 2; 3], [5; 6; 7]);
 T = table (Name, Age, Gender, Visit)
T =
  3x4 table

      Name       Age    Gender       Visit       
    _________    ___    ______    ___________    

    "Sanchez"     38         M    05-Jan-2024    
    "Johnson"     43         M    06-Feb-2024    
    "Li"          40         F    07-Mar-2024
 removevars (T, 'Gender')
ans =
  3x3 table

      Name       Age       Visit       
    _________    ___    ___________    

    "Sanchez"     38    05-Jan-2024    
    "Johnson"     43    06-Feb-2024    
    "Li"          40    07-Mar-2024

Several variables can go at once, here selected by their column numbers.

 removevars (T, [2, 4])
ans =
  3x2 table

      Name       Gender    
    _________    ______    

    "Sanchez"         M    
    "Johnson"         M    
    "Li"              F