Filter table based on time and make calculation
2 views (last 30 days)
Show older comments
Dion Theunissen
on 19 Jul 2021
Edited: Scott MacKenzie
on 19 Jul 2021
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 Comments
the cyclist
on 19 Jul 2021
Edited: the cyclist
on 19 Jul 2021
... 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.
Accepted Answer
Scott MacKenzie
on 19 Jul 2021
Edited: Scott MacKenzie
on 19 Jul 2021
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 Comments
More Answers (0)
See Also
Categories
Find more on Timetables 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!