How to create a series of dates that only gives me the end of the months?

advertisements

How do I create a dates series that gives me only the end of the month days? i.e.

31/01/1993
28/02/1993
...
31/01/2013


Use calendar function:

for year = 1993:2013
    for month = 1:12
        lastday = max(max(calendar(year,month)));
        fprintf('%02d/%02d/%04d\n',lastday,month,year)
    end
end

You can of course store result in matrix (or cell array) by changing fprintf with sprintf:

dates = char(zeros(12*21,10));
idx = 0;
for year = 1993:2013
    for month = 1:12
        idx = idx + 1;
        lastday = max(max(calendar(year,month)));
        dates(idx,:) = sprintf('%02d/%02d/%04d',lastday,month,year);
    end
end

---EDIT---

I've found thath there is aslo eomday function. So instead of line:

lastday = max(max(calendar(year,month)));

You can use:

lastday = eomday(year,month);

To see the algorithm of eomday function type in MATLAB:

open eomday