vartype
Selects table variables by data type, for use as a subscript.
Category: Language Fundamentals > Data Types
Syntax
S = vartype(type)
Creates a subscript that selects variables of the given type.
Description
vartype is a convenient way to apply an operation to one kind of column without naming them. For example T(:, vartype('numeric')) keeps only the numeric columns, and varfun(@mean, T, 'InputVariables', vartype('numeric')) averages only those.
Input arguments
type'numeric','categorical','datetime','duration','logical','cellstr','string','char', or a class name.
Output arguments
S- Subscript object, used as
T(:, vartype('numeric')).
Example
Keep numeric columns
T = table([1;2], {'a';'b'}, 'VariableNames', {'n','s'});
T(:, vartype('numeric'))
Output
ans = 2×1 table
n
_
1
2