Separate yearly data into months in different columns
2 views (last 30 days)
Show older comments
% I have 5 years of data arranged in such a way that all data is on daily basis in one column. Means 1 value of each day, if one year comprises of 365 days then 5*365=1825 + 1 (+1 because year 2000 is a leap year); that makes a total of 1826 values.
% I want all the months in separate columns for instance, Jan 1999 in one column then Feb 1999 in 2nd and so on till Dec 2003.
% I saw that we have got a function on MATLAB which is "tomonthly" but prior to that we need to use "fints" and for using fints, dates and data in separate columns is required, for fulfilling this requirement I created a whole range of datetime, then I created its vector using datenum,
% Then I used fints, you can see the output that the data is arranged according to the appropriate dates, then I used the function, "tomonthly" but it had just picked up the last and somewhere the second last days of the months and their values.
% Whereas, my requirement is to have all the monthly data in separate columns. If the interval was same, that would be much easier through loops but here the months are of 28,30 and 31 days, secondly, due to feb in leap year, it is having 29 days.
% I will be very grateful if someone help me out in this solution. I have to apply the same thing on a huge set of years, therefore, I need to do it through code. % The code snippet which I tried is mentioned below:
load Data % 1826x1 double
d_1=datetime(1999,01,01);
d_2=datetime(2003,12,31);
Dates=d_1:d_2;
Datecols=Dates'; %5 years dates range
datesnew=datenum(Datecols); %dates to serial date numbers
A=fints(datesnew,Data);
Monthlydata=tomonthly(A); %conversion
1 Comment
Guillaume
on 13 Mar 2018
What is the reason behind wanting to have monthly columns? My first instinct is that it's going to make subsequent processing harder rather than easier.
What should go in row 29-30-31 for the shorter months?
Having read the help of the twomonthly function of the financial toolbox, it's not designed to what you want at all. It reduces the data to just one value per month.
Answers (2)
ANKUR KUMAR
on 13 Mar 2018
"Jan 1999 in one column then Feb 1999 in 2nd and so on till Dec 2003......" This never be possible because the length of elements in Jan 1999 and Feb 1999 are not same and it's not possible to store in rows of varying dimensions. Better to store in cell array.
Here is my hint to your problem. Hope this helps you.
clc
clear
A=randi(25,1826,1);
id_1999=[31 28 31 30 31 30 31 31 30 31 30 31];
id_2000=[31 29 31 30 31 30 31 31 30 31 30 31];
id_2001=id_1999;
id_2002=id_1999;
id_2003=id_1999;
id=[id_1999' ;id_2000' ;id_2001'; id_2002' ;id_2003'];
iid=[0; cumsum(id)];
for ii=1:length(id)
AA{ii}=A(iid(ii)+1:iid(ii+1)) ;
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!