MATLAB Answers

Filter table based on time and make calculation

조회 수: 4(최근 30일)
Dion Theunissen
Dion Theunissen 2021년 7월 19일
편집: Scott MacKenzie 2021년 7월 19일
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
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
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개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by