varfun

Applies a function to the variables of a table.

Category: Language Fundamentals > Data Types

Syntax

B = varfun(func, A)

Applies func to each variable of A and returns the results as a table.

B = varfun(func, A, 'InputVariables', vars)

Applies func only to the chosen variables.

Description

varfun computes a summary of every column at once: totals, means, minima. With a function that returns a column of the same height, the result has the same number of rows as A, as with varfun(@(x) x*2, A), whose variables are named Fun_a, Fun_b when the function is anonymous.

Input arguments

func
Function handle that takes a column and returns a value.
A
Table or timetable.
'InputVariables'
Names or positions of the variables to use.

Output arguments

B
Table with one variable per input variable, named like sum_a after the function and the variable.

Example

Column summaries

T = table([1;2;3], [4;5;6], 'VariableNames', {'a','b'});
varfun(@sum, T)
varfun(@mean, T, 'InputVariables', 'b')
Output
ans = 1×2 table

    sum_a    sum_b
    _____    _____

      6       15  
ans = 1×1 table

    mean_b
    ______

      5

See also

rowfun groupsummary splitapply cellfun table