How to SUMIF similar to Excel

조회 수: 29 (최근 30일)
alex
alex 2022년 5월 3일
답변: Stephen23 2022년 5월 3일
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

답변 (1개)

Stephen23
Stephen23 2022년 5월 3일
"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

카테고리

Help CenterFile Exchange에서 Waveform Generation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by