Summing elements in an array between uneven date intervals
2 views (last 30 days)
Show older comments
I have a dataset of daily rainfall. I also have a list of 28 days on which environmental sampling was carried out. I'm trying to figure out how to sum the total precipitation that fell between each environmental sampling (represented by "sample_dates")
The environmental samplings are mostly about 30 days apart, but not exactly. I already know how to find the monthly precipitation, but what I want is the exact amount of rainfall that fell between each sampling.
Any advice on what functions to use would be so appreciated. I've read documentation for cumsum, ismember, movsum, and a bunch of previously asked questions but I can't find anything that's helped me come up with a solution.
T = readtable("precip_test.csv", "VariableNamingRule","preserve");
A = table2array(T);
%sequential dates from Sept 2019 to Dec 2022
rain_dates = A(:,1);
%daily precipitation in mm for each day
rain_mm = A(:,2);
%dates that environmental samples were taken
sample_dates = A(:,3);
sample_dates(any(isnan(sample_dates), 2), :) = [];
0 Comments
Accepted Answer
Star Strider
on 6 Oct 2023
Edited: Star Strider
on 6 Oct 2023
I am not certain what you want. If I am readiing the file correctly and converting the dates correctly (both 'excel' and 'posixtime' give reasonable results for the dates, however the ‘Date Time’ and ‘Sample Dates’ do not make sense with 'excel') the ‘Sample Dates’ seem to give appropriate results with ‘Precip (mm)’ so it would seem that with that we are finished and nothing further need be done.
T = readtable("precip_test.csv", "VariableNamingRule","preserve")
DateTime = datetime(T.Date, 'ConvertFrom','excel');
SampleDates = datetime(T.('Sample dates'), 'ConvertFrom','excel');
T2 = table(DateTime, T.('Precip (mm)'), SampleDates, 'VariableNames',{'Date Time','Precip (mm)','Sample Dates'})
T2 = rmmissing(T2); % Remove 'NaT' Rows
LastLine = T2(end,:)
VN = T2.Properties.VariableNames;
figure
stairs(T2.('Date Time'), T2.('Precip (mm)'))
grid
xlabel(VN{3})
ylabel(VN{2})
return % Stop Here
A = table2array(T);
%sequential dates from Sept 2019 to Dec 2022
rain_dates = A(:,1);
%daily precipitation in mm for each day
rain_mm = A(:,2);
%dates that environmental samples were taken
sample_dates = A(:,3);
sample_dates(any(isnan(sample_dates), 2), :) = [];
What else do you want to do with these resullts?
.
15 Comments
More Answers (0)
See Also
Categories
Find more on Calendar 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!

