- datetime - https://www.mathworks.com/help/matlab/ref/datetime.html
- dateshift - https://www.mathworks.com/help/matlab/ref/datetime.dateshift.html
- groupsummary - https://www.mathworks.com/help/matlab/ref/double.groupsummary.html
Sum up measurements to daily basis (measurements each 10 minutes)
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I want to sum up measurements (of precipitation), that is measured each 10 minutes in one month, to a daily basis.
Sometimes there is a whole in the dataset, but each measurement is related to a date in the format 'yyyy-mm-ddTHH:MM:SSZ'.
I have already converted the date format to a number with the help of the function datenum.
The time and measurements is in a table of the size 4464x2, which corresponds to measurements each 10 minutes in one month (other datasets is of the size 2518x2).
Column 1 is the time and column 2 is the amount of precipitation.
How do I easily sum up, so that I get the amount of precipitation in one day over a month (corresponding to 30 to 31 rows)?
0 个评论
回答(1 个)
ag
2024-10-3
Hi Emma,
To sum up the precipitation measurements on a daily basis from a dataset where each measurement is taken every 10 minutes, you can use MATLAB's "dateshift" function to extract the date from the datetime and then use MATLAB's "groupsummary" function to sum the precipitation for each day.
The below code snippet explains how to achieve this using dummy data:
%dummy data
numMeasurements = 4464;
startDate = datenum(2023, 1, 1, 0, 0, 0);
datenumArray = startDate + (0:10:(numMeasurements-1)*10)'/1440;
precipitationArray = rand(numMeasurements, 1); % Random precipitation values
T = table(datenumArray, precipitationArray, 'VariableNames', {'Datenum', 'Precipitation'});
% Convert 'Datenum' to 'datetime'
T.Time = datetime(T.Datenum, 'ConvertFrom', 'datenum');
% Add a new column to group by date
T.Date = dateshift(T.Time, 'start', 'day');
% Sum the precipitation for each day
dailyPrecipitation = groupsummary(T, 'Date', 'sum', 'Precipitation');
% Display the result
disp(dailyPrecipitation);
For more details, please refer to the following MathWorks documentations:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!