Summing columns of table

210 views (last 30 days)
Leonor Vieira Dias
Leonor Vieira Dias on 29 Jan 2021
Commented: Mathieu NOE on 29 Jan 2021
Hello,
I have imported a table from excel to Matlab in it's correct form.
I have already summed the first two columns and added as new column to table by doing:
T = readtable("Table 1.xlsx")
T.Early = (T.x22_early) + (T.x7_early)
However, the rest of the table is very long. I was wondering if there is a simpler way than to enter all the columns individually as I did previously.
Thank you in advance

Accepted Answer

Mathieu NOE
Mathieu NOE on 29 Jan 2021
hello
see example for taking mean values :
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)
T=10×5 table
Gender Test1 Test2 Test3 TestAvg
__________ _____ _____ _____ _______
HOWARD {'male' } 90 87 93 90
WARD {'male' } 87 85 83 85
TORRES {'male' } 86 85 88 86.333
PETERSON {'female'} 75 80 72 75.667
GRAY {'female'} 89 86 87 87.333
RAMIREZ {'female'} 96 92 98 95.333
JAMES {'male' } 78 75 77 76.667
WATSON {'female'} 91 94 92 92.333
BROOKS {'female'} 86 83 85 84.667
KELLY {'male' } 79 76 82 79
now do it for sum , and specify column index if needed :
T.TestSum = sum(T{:,2:end},2)
  2 Comments
Leonor Vieira Dias
Leonor Vieira Dias on 29 Jan 2021
Briliant, it worked! Thank you
Mathieu NOE
Mathieu NOE on 29 Jan 2021
you're welcome !!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!