%% Calculations on Tables
% This example shows how to perform calculations on tables.
% The functions rowfun and varfun each apply a specified function to a table, yet many other functions require numeric or homogeneous arrays as input arguments. You can extract data from individual variables using dot indexing or from one or more variables using curly braces. The extracted data is then an array that you can use as input to other functions.
%% Read Sample Data into Table
% Read data from a comma-separated text file, testScores.csv, into a table using the readtable function. testScores.csv contains test scores for several students. Use the student names in the first column of the text file as row names in the table.

T = readtable('testScores.csv','ReadRowNames',true)

% T is a table with 10 rows and four variables.
%
%% Summarize the Table
% View the data type, description, units, and other descriptive statistics for each variable by using the summary function to summarize the table.

summary(T)

% The summary contains the minimum, median, and maximum score for each test.
%
%% Find the Average Across Each Row
% Extract the data from the second, third, and fourth variables using curly braces, {}, find the average of each row, and store it in a new variable, TestAvg.

T.TestAvg = mean(T{:,2:end},2)

% Alternatively, you can use the variable names, T{:,{'Test1','Test2','Test3'}} or the variable indices, T{:,2:4} to select the subset of data.
%
%% Compute Statistics Using Grouping Variable
% Compute the mean and maximum of TestAvg by gender of the students.

varfun(@mean,T,'InputVariables','TestAvg',...
       'GroupingVariables','Gender')

%
%% Replace Data Values
% The maximum score for each test is 100. Use curly braces to extract the data from the table and convert the test scores to a 25 point scale.

T{:,2:end} = T{:,2:end}*25/100

%
%% Change Variable Name
% Change the variable name from TestAvg to Final.

T.Properties.VariableNames{end} = 'Final'

% Copyright 2012 The MathWorks, Inc.