Filter table based on time and make calculation
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dion Theunissen
le 19 Juil 2021
Modifié(e) : Scott MacKenzie
le 19 Juil 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 commentaires
the cyclist
le 19 Juil 2021
Modifié(e) : the cyclist
le 19 Juil 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.
Réponse acceptée
Scott MacKenzie
le 19 Juil 2021
Modifié(e) : Scott MacKenzie
le 19 Juil 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Timetables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!