Only month and year matrix

12 views (last 30 days)
shalenys bedoya
shalenys bedoya on 1 Dec 2015
Commented: dpb on 2 Dec 2015
Hello! Can anybody help me with a datenum problem? I want to make an only month and year matrix, but I dont find a way to do this with datenum function, because makes me put the day. My dataset just have month, year and temperature. I tried to make a matrix of mm,yyy and dd and later make an bucle that gives me just one day of each month for each year (1946-2003), but then, when I make a matrix of date and temperature, matlab gives me the matrix with cientific notation.
Pd. When I put datenum(year,month,'mm/yy'), I got an error with datenum.

Answers (1)

dpb
dpb on 1 Dec 2015
Use the facility of datenum to automagically roll over the higher-granularity fields...
dn=datenum(1946,[1:((2003-1946)+1)*12].',1);
It's simple for the fields that are fixed number of units such as months as every year has twelve months; for others one has to sometimes compute the number of days in the year or month,year, but the same idea works.
>> dn=datenum(1946,[1:((2003-1946)+1)*12].',1);
>> [y,m]=datevec([dn(1:3);dn(end-5:end)]);
>> [m y]
ans =
1 1946
2 1946
3 1946
7 2003
8 2003
9 2003
10 2003
11 2003
12 2003
>>
I'm not sure if the new date datatype has a better way to generate vectors of dates from first/last dates or not; it might have some better facilities.
  1 Comment
dpb
dpb on 2 Dec 2015
OK, looked at R2014b+ datetime; it doesn't include any way to do periodic dates automagically, either; thought it might. What it does do, however, is have a display feature that values are displayed as dates rather than date numbers which is the root cause of the above complaint re: "scientific notation". The above solution is, of course, also datenum so the values of dn are doubles of some large integer value; you manipulate them for display via datestr or numerically knowing what they represent or as above you can generate the corresponding date vectors.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!