table.varfun
table: B = varfun (func, A)
table: B = varfun (func, A, Name, Value, …)
Apply a function to each variable of a table.
B = varfun (func, A) applies the function
handle func separately to each variable of the table A and
returns the results in the table B. func is called once per
variable with that variable as its single input argument. By default
each output variable of B is named f_v, where
f is the name of func (or Fun when func is
anonymous) and v is the name of the corresponding variable of
A.
B = varfun (func, A, Name, Value,
…) modifies the operation through the following
Name/Value pairs:
'InputVariables'true for the variables to include. By default func
is applied to every variable of A that is not a grouping variable.'GroupingVariables'GroupCount
variable holding the number of rows in each group. Rows with a missing
value in any grouping variable are omitted.'OutputFormat''auto' (the default, equivalent to
'table'), 'table', 'uniform', or 'cell'.
For 'uniform', func must return a scalar on each call and
the results are concatenated into an array. For 'cell' the
results are returned in a cell array. The 'uniform' and
'cell' formats return only the results of func, without the
grouping variables or GroupCount.'ErrorHandler'identifier, message, and
index, followed by the same inputs that were passed to
func, and its outputs are used in place of the outputs of
func.Source Code: table
varfun applies a function to each variable of a table — a column-wise map. The result is itself a table, its variable names prefixed by the function.
T = table ([38; 43; 40], [71; 69; 64], 'VariableNames', {'Age', 'Height'})
T =
3x2 table
Age Height
___ ______
38 71
43 69
40 64
varfun (@mean, T)
ans =
1x2 table
mean_Age mean_Height
________ ___________
40.3333 68
Ask for a plain array instead of a table with 'OutputFormat'.
varfun (@mean, T, 'OutputFormat', 'uniform')
ans = 40.333 68.000