calculating the number of days within each month for a range of dates
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
CB = [datenum([2011,07,13]), datenum([2012,02,19]);
datenum([2012,05,03]), datenum([2013,03,21]);
datenum([2013,06,06]),datenum([2013,09,05]);
datenum([2013,09,05]),datenum([2014,04,28]);
datenum([2014,04,29]),datenum([2014,09,09]);
datenum([2014,09,09]),datenum([2015,05,02]);
datenum([2015,05,01]),datenum([2015,09,06])
datenum([2017,04,30]),datenum([2017,09,12])];
PT = [datenum([2012,09,09]), datenum([2013,06,10]);
datenum([2013,06,11]), datenum([2013,08,20]);
datenum([2013,09,03]),datenum([2014,03,21]);
datenum([2014,04,30]),datenum([2014,09,10])];
QN = [datenum([2013,06,10]), datenum([2013,09,11]);
datenum([2013,09,11]), datenum([2014,04,16]);
datenum([2014,09,10]),datenum([2015,05,02]);
datenum([2015,05,02]),datenum([2015,08,18]);
datenum([2017,04,30]),datenum([2017,09,14])];
AB = [datenum([2017,04,28]), datenum([2017,09,14])];
KS = [datenum([2010,06,03]), datenum([2010,07,20])];
BD = [ datenum([2010,08,27]), datenum([2011,05,26]);
datenum([2011,05,31]),datenum([2012,08,26])];
OCNMS = [ datenum([2007,07,03]),datenum([2008,06,15]);
datenum([2011,01,27]),datenum([2011,10,07]);
datenum([2011,12,07]),datenum([2012,07,11]);
datenum([2012,09,14]),datenum([2013,06,30]);
datenum([2013,07,17]),datenum([2014,05,02])];
DCPP01C = [datenum([2012,11,07]), datenum([2013,03,19])];
CCE = [datenum([2016,10,10]), datenum([2017,11,08])];
HOKE = [datenum([2008,09,15]), datenum([2009,06,06])];
CORC = [datenum([2014,05,01]),datenum([2015,12,04])];
CBtext = repmat({'CB'},size(CB,1),1);
CB2 = [CBtext num2cell(CB)];
PTtext = repmat({'PT'},size(PT,1),1);
PT2 = [PTtext num2cell(PT)];
QNtext = repmat({'QN'},size(QN,1),1);
QN2 = [QNtext num2cell(QN)];
ABtext = repmat({'AB'},size(AB,1),1);
AB2 = [ABtext num2cell(AB)];
KStext = repmat({'KS'},size(KS,1),1);
KS2 = [KStext num2cell(KS)];
BDtext = repmat({'BD'},size(BD,1),1);
BD2 = [BDtext num2cell(BD)];
OCNMStext = repmat({'OCNMS'},size(OCNMS,1),1);
OCNMS2 = [OCNMStext num2cell(OCNMS)];
DCPP01Ctext = repmat({'DCPP01C'},size(DCPP01C,1),1);
DCPP01C2 = [DCPP01Ctext num2cell(DCPP01C)];
CCEtext = repmat({'CCE'},size(CCE,1),1);
CCE2 = [CCEtext num2cell(CCE)];
HOKEtext = repmat({'HOKE'},size(HOKE,1),1);
HOKE2 = [HOKEtext num2cell(HOKE)];
CORCtext = repmat({'CORC'},size(CORC,1),1);
CORC2 = [CORCtext num2cell(CORC)];
D = [CB2;PT2;QN2;AB2;KS2;BD2;OCNMS2;DCPP01C2;CCE2;HOKE2;CORC2];
Dtab = cell2table(D);
datetime.setDefaultFormats('default','MM-dd-yyy');
Dtab.D2 = datetime(Dtab.D2,'ConvertFrom','datenum');
Dtab.D3 = datetime(Dtab.D3,'ConvertFrom','datenum');
Dtab.Total_Days = days(Dtab.D3-Dtab.D2);
I have a table with a range of dates (Dtab) and I'm trying to find out how many days fall within each month. For example, the first row has 221 days between 7/13/11 and 2/19/2012. I want to know how many days in January, February, etc. Eventually, I want to sort it by season (Winter = Dec - Feb) to find out how much effort I had in each season. Thanks in advance for any help!
2 commentaires
the cyclist
le 7 Oct 2019
Can you upload the data, instead of an image of the data? That saves us the work of recreating something to test an algorithm on.
Réponse acceptée
Guillaume
le 7 Oct 2019
You've got to learn to work with datetime arrays. There's no need to use datenum to construct datetimes, and adjusting the default formatting is not really the proper way of setting the format of individual arrays.
Anyway, assuming R2018a for splitvars:
Dtab = [Dtab, ...
splitvars(rowfun(@(s, t) accumarray(month(s:t)', 1, [12 1])', Dtab, 'InputVariables', {'D2', 'D3'}), ...
1, 'NewVariableNames', datetime.MonthsOfYear.Short)]
I'm also using the undocumented hidden constant property MonthsOfYear of datetime to get the months name. If you don't like that you can hardcode them or get the names another way.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Dates and Time 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!