Main Content

vartype

Subscript into table or timetable by variable type

Description

example

S = vartype(type) creates a subscript to select table variables of a specified type. The type input argument is a character vector that specifies any type that is accepted by the isa function, such as 'numeric', 'float', 'integer', or 'string'. It also can be 'cellstr' to select variables that contain cell arrays of character vectors.

For example, S = vartype('numeric'); T2 = T1(:,S) returns T2 as a table that contains only the numeric variables from the table T1.

Examples

collapse all

Create a table that contains numeric and string variables. Then subscript into the table to get only its numeric variables.

LastName = ["Smith";"Johnson";"Williams";"Jones";"Brown"];
Age = [38;43;38;40;49];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Height,Weight,BloodPressure)
T=5×5 table
     LastName     Age    Height    Weight    BloodPressure
    __________    ___    ______    ______    _____________

    "Smith"       38       71       176       124     93  
    "Johnson"     43       69       163       109     77  
    "Williams"    38       64       131       125     83  
    "Jones"       40       67       133       117     75  
    "Brown"       49       64       119       122     80  

Create a subscript with the vartype function. Subscript into the second dimension of T to return a table that contains only the numeric variables.

S = vartype('numeric');
T2 = T(:,S)
T2=5×4 table
    Age    Height    Weight    BloodPressure
    ___    ______    ______    _____________

    38       71       176       124     93  
    43       69       163       109     77  
    38       64       131       125     83  
    40       67       133       117     75  
    49       64       119       122     80  

You can create a subscript for any type that the isa function accepts. Select the string variable from T.

S = vartype('string');
T3 = T(:,S)
T3=5×1 table
     LastName 
    __________

    "Smith"   
    "Johnson" 
    "Williams"
    "Jones"   
    "Brown"   

Create a timetable that contains numeric, string, and categorical variables. Then subscript into the table to get only its numeric variables.

Date = datetime(["12/18/2015";"12/19/2015";"12/20/2015"]);
Temp = [45;33;36];
Pressure = [30.1;29.3;29.7];
Location = ["Boston";"Boston";"Worcester"];
SensorType = categorical(["S1";"X7";"S1"]);
TT = timetable(Date,Temp,Pressure,Location,SensorType)
TT=3×4 timetable
       Date        Temp    Pressure     Location      SensorType
    ___________    ____    ________    ___________    __________

    18-Dec-2015     45       30.1      "Boston"           S1    
    19-Dec-2015     33       29.3      "Boston"           X7    
    20-Dec-2015     36       29.7      "Worcester"        S1    

Create a subscript with the vartype function. Subscript into the second dimension of TT to return a timetable that contains only the numeric variables. TT2 also has the row times from TT because the times identify the rows. The vector of row times is a property of the timetable, and not one of its variables.

S = vartype('numeric');
TT2 = TT(:,S)
TT2=3×2 timetable
       Date        Temp    Pressure
    ___________    ____    ________

    18-Dec-2015     45       30.1  
    19-Dec-2015     33       29.3  
    20-Dec-2015     36       29.7  

Input Arguments

collapse all

Type of variables to select from a table or a timetable, specified as a character vector or string scalar. type can be any type or category that is accepted by the isa function. It also can be 'cellstr'.

Extended Capabilities

Thread-Based Environment
Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

Version History

Introduced in R2016b