table

Methods

Method Reference: 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'
The variables of A to which func is applied, given as variable names, indices, a logical vector, or a function handle that returns true for the variables to include. By default func is applied to every variable of A that is not a grouping variable.
'GroupingVariables'
One or more variables of A that define groups of rows. When grouping variables are given, func is applied to the values of each input variable within each group, B has one row per group, and B also includes the grouping variables and a GroupCount variable holding the number of rows in each group. Rows with a missing value in any grouping variable are omitted.
'OutputFormat'
The format of B, one of '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'
A function handle that is called when func throws an error. It receives a structure with fields 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

Example: 1

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