How to SUMIF similar to Excel
35 views (last 30 days)
Show older comments
Hi I have a table with the following values and want to do SUMIF similar to excel and save data to a new table (Table T below). I tried to get all power values that occur at the same time to be summed, so that I can plot one curve. Not sure where this is going wrong.
DateTime Power
____ _____
Feb 1 2022 01:00 4
Feb 1 2022 02:00 5
Feb 1 2022 02:00 2
Feb 1 2022 03:00 1
Desired Result:
DateTime Power
____ _____
Feb 1 2022 01:00 4
Feb 1 2022 02:00 7
Feb 1 2022 03:00 1
t1 = datetime(2022,02,01,0,1,0);
t2 = datetime(2022,02,01,0,5,0);
totalminutes = minutes(t2-t1);
t = (t1:minutes(1):t2);
for i = 1:totalminutes;
if T.DateTime == t(1,i);
R = table(T.DateTime,sum(T.Power));
else
end
0 Comments
Answers (1)
Stephen23
on 3 May 2022
"Not sure where this is going wrong."
Reinventing the wheel by writing lots of loops, and ignoring the inbuilt tools.
dt = datetime(2022,2,1,[1;2;2;3],0,0);
pw = [4;5;2;1];
tbl = table(dt,pw)
out = groupsummary(tbl,"dt","sum")
0 Comments
See Also
Categories
Find more on Waveform Generation 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!