I had some errors in my previous version. Here the logic is included in the function. It also checks the month and updates accordingly.
function out = roundMonth(dateNumber) dateVector = datevec(dateNumber); day = dateVector(3); month = dateVector(2); year = dateVector(1); month = month + sign(day - 15 + double(~(month-2)))... + double(~(day-15 + double(~(month-2)))); dateVector(1) = year + double((month-12)==1) - double((1-month)==1); dateVector(2) = mod(month,12) + 12*double(~mod(month,12)); out = datestr(dateVector,'mmm yyyy');
EXAMPLES
1.
roundMonth(datenum('10-Oct-2010')) ans = Sep 2010
2.
roundMonth(datenum('20-Oct-2010')) ans = Nov 2010
3.
roundMonth(datenum('20-Dec-2010')) ans = Jan 2011
4.
roundMonth(datenum('10-Jan-2010')) ans = Dec 2009
source share