How to SUMIF similar to Excel

56 visualizaciones (últimos 30 días)
alex
alex el 3 de Mayo de 2022
Respondida: Stephen23 el 3 de Mayo de 2022
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

Respuestas (1)

Stephen23
Stephen23 el 3 de Mayo de 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)
tbl = 4×2 table
dt pw ____________________ __ 01-Feb-2022 01:00:00 4 01-Feb-2022 02:00:00 5 01-Feb-2022 02:00:00 2 01-Feb-2022 03:00:00 1
out = groupsummary(tbl,"dt","sum")
out = 3×3 table
dt GroupCount sum_pw ____________________ __________ ______ 01-Feb-2022 01:00:00 1 4 01-Feb-2022 02:00:00 2 7 01-Feb-2022 03:00:00 1 1

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by