FOR Loop _ Sum columns in Excel
Show older comments
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
2 Comments
dpb
on 6 Aug 2019
Show what
whos BPAQ
returns and
BPAQ.Properties.VariableNames
It's not clear to me what you're actually trying to sum and what you're trying to describe about the sum being repetitive.
Show us a small example of what you have and what you're trying to compute...
Ted Dang
on 7 Aug 2019
Accepted Answer
More Answers (1)
As one alternative solution to S S, there's the readtable route...
t=readtable('BPAQ.xlsx'); % read .xlsx file to table, t
ix=find(ismember(t.Properties.VariableNames,{'bps_1','bps_29'})); % find location of given variables
t.bps=t{:,ix(1):ix(2)}; % replace named variables bps_n w/ array bps
t(:,ix(1):ix(2))=[]; % remove the now superfluous named ones
t.bpsmean=mean(t.bps,2,'omitnan'); % compute row means excluding NaN
leaves you with...
>> t
t =
8×7 table
sudscreen_studyid redcap_event_name date_bpaq bps_score q_bpaq_complete bps bpsmean
_________________ _________________ _________ _________ _______________ _____________ _______
1 'baseline1_arm_1' NaN NaN 2 [1x29 double] 4
2 'baseline1_arm_1' NaN NaN 2 [1x29 double] 4
3 'baseline1_arm_1' NaN NaN 2 [1x29 double] 4
4 'baseline1_arm_1' NaN 70 2 [1x29 double] 2.5926
5 'baseline1_arm_1' NaN NaN 0 [1x29 double] 4
6 'baseline1_arm_1' NaN NaN 2 [1x29 double] 2.2222
7 'baseline1_arm_1' NaN NaN 2 [1x29 double] 1.5185
8 'baseline1_arm_1' NaN NaN 0 [1x29 double] 4
>>
4 Comments
Ted Dang
on 7 Aug 2019
"...logic why you come up with this."
Precisely because almost every time you have variables with sequentially-generated suffixes for names or the like, in MATLAB they invariably should be an array instead. Doing that makes subsequently operating on either the array or subsets of the array essentially painless as computing the row means here illustrates.
The only difficulty arises in programmatically creating the array from the named variables in the first place -- here, since the original post didn't indicate this was just a very small subset of a much larger dataset, I hardcoded the first/last names in the lookup to locate their position in the table.
"In anger" it would be better to find that size dynamically as well, and if the size is potentially variable, almost mandatory. This is simple enough in principle as well as long as there is a way to uniquely identify which variables are those which belong to the array set--here a regular expression of bps_digits would suffice.
Ted Dang
on 8 Aug 2019
dpb
on 8 Aug 2019
You're welcome, certainly! Trying to teach as well as "just answer" is what I enjoy most...and so I'll typically try to push learners to do more than just enough to get by, but to begin to use the power inherent in Matlab...that takes some effort, granted, but is effort well repaid.
To see what's going on above, you can take pieces of the indexing expressions out and just evaluate them on their own -- then much of the mystery will disappear.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!