%% Apply Element-wise Function
% Define and apply an element-wise function to the variables of a table to square all the elements.
% Define a table containing numeric variables.

A = table([0.71;-2.05;-0.35;-0.82;1.57],[0.23;0.12;-0.18;0.23;0.41])

%
% Define the anonymous function to find the square of an input.

func = @(x) x.^2;

%
% Apply the function to all the variables of table A.

B = varfun(func,A)

% The variables of B have names based on the function and the variable names from A.
% Copyright 2012 The MathWorks, Inc.