table

Methods

Method Reference: table.rowfun

table: B = rowfun (func, A)
table: B = rowfun (func, A, Name, Value, …)

Apply a function to each row of a table.

B = rowfun (func, A) applies the function handle func to each row of the table A and returns the results in the table B, which has one row for each row of A. By default the value of each variable in the row is passed to func as a separate input argument, and the output variables of B are named Var1, Var2, and so on.

B = rowfun (func, A, Name, Value, …) modifies the operation through the following Name/Value pairs:

'InputVariables'
The variables of A that are passed to func, given as variable names, indices, a logical vector, or a function handle. By default every variable of A that is not a grouping variable is used.
'GroupingVariables'
One or more variables of A that define groups of rows. When grouping variables are given, func is applied once to each group, receiving the values of each input variable across the rows of the group; B has one row per group and also includes the grouping variables and a GroupCount variable. Rows with a missing value in any grouping variable are omitted.
'OutputVariableNames'
The names of the output variables of B, one per output of func.
'NumOutputs'
The number of output arguments to request from func. It defaults to the number of 'OutputVariableNames' if those are given, otherwise to 1.
'SeparateInputs'
A logical scalar. When true (the default), the value of each input variable is passed to func as a separate argument. When false, the values of the row are horizontally concatenated and passed as a single argument.
'ExtractCellContents'
A logical scalar. When true, the contents of cell-valued variables are extracted before being passed to func. It defaults to false.
'OutputFormat'
The format of B, one of 'auto' (the default, equivalent to 'table'), 'table', 'uniform', or 'cell'. For 'uniform', every call to func must return scalars of the same type, which 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.
'ErrorHandler'
A function handle that is called when func throws an error, receiving a structure with fields identifier, message, and index followed by the inputs passed to func.

Source Code: table

Example: 1

rowfun applies a function to each row, passing that row's variables as separate arguments — a row-wise map, complementing the column-wise varfun.

 Weight = [176; 163; 133];
 Height = [71; 69; 64];
 T = table (Weight, Height)
T =
  3x2 table

    Weight    Height    
    ______    ______    

       176        71    
       163        69    
       133        64
 rowfun (@(w, h) 703 * w / h^2, T, 'OutputVariableNames', {'BMI'})
ans =
  3x1 table

      BMI      
    _______    

    24.5443    
    24.0683    
    22.8269