Filter table based on time and make calculation
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a table like this below:
Now i want to make a loop that i can calculate the mean of a specific column based on the time. So I want to calculate the mean of 29th of june, 30th of june and so on.
Anyone who can help me with this?
2 个评论
the cyclist
2021-7-19
编辑:the cyclist
2021-7-19
... specificaly, upload the data in a MAT file, not just an image (which we cannot work with). If you cannot upload the whole file for some reason, just several rows that cover multiple dates would be enough.
采纳的回答
Scott MacKenzie
2021-7-19
编辑:Scott MacKenzie
2021-7-19
You don't need a loop. At a high level, here's what you need to do. Read the data into MATLAB, probably into a table using the readtable function. From there you build a timetable with one column for the date and time and other columns for the data you want to summarze. Then, use the retime function to build the summaries of interest at the desired granularity (e.g., daily). The code below does this with some random test data.
% create a datetime vector starting in June with one entry every 30 minutes
t1 = datetime(2021, 6, 1);
t2 = datetime('now');
dt = t1:hours(0.5):t2;
% create some test data, similar to data in the question
speed = rand(1,length(dt));
distance = rand(1,length(dt));
% combine the datetime elements and test data into a timetable
TT1 = timetable(dt', speed', distance', 'variablenames', {'Speed', 'Distance'} );
% compute daily means for speed and distance
TT2 = retime(TT1, 'daily', 'mean');
% plot mean speed by day
plot(TT2.Time, TT2.Speed, '-bo', 'markerfacecolor', 'b');
xlabel('Date');
ylabel('Speed (units)');
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Timetables 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!