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 Center および File ExchangeWaveform Generation についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by