MATLAB: FOR Loop _ Sum columns in Excel

#forloop #sum #columns #excel

I'm trying to calculate the sum of few columns in an excel file. Each row is each participant so the Sum will be repetitve Row times (7), BPAQ is the filename
When I hit run this script, the output is not the Sum but two new variables in the workspace with only one number inside.
Can someone help me figure this out and Am I using the right funtion to sum columns in table?
Thank you so much.
I've also attached the screenshot
for i=1:1:7
score_BPAQ = sum(BPAQ{i,["bps_1","bps_29"]});
end

Best Answer

Try this:
[D,S] = xlsread('BPAQ.xlsx');
BPAQ = D(:,5:33);
BPAQ(isnan(BPAQ)) = 0; % Set ‘NaN’ Values To 0
RowSum = sum(BPAQ,2) % Sum For Each PArticipant
Tot = sum(RowSum) % Sum For All Participants
producing:
RowSum =
116
116
116
70
116
60
41
116
Tot =
751