MATLAB: Indexing by year from full date

datesindexing

I'm working with a data set that provides the date in two ways, yyyymmdd (bot_date) and decimal years (bot_decy). I need to be able to index the data by year (and month) but I'm having trouble separating the dates into year, month, and day.
[bot_yr bot_mon bot_day bot_hr bot_min bot_sec]=datevec(bot_decy);
bot_V=datevec(bot_decy);
%ConvertSerialYearToDate(bot_decy);
%[bot_yr bot_mon bot_day bot_hr bot_min bot_sec]=datevec(bot_decy);
%bot_V=datevec(bot_decy);
%BD=datevec(bot_decy);
function [num] = ConvertSerialYearToDate( y )
year = floor(y);
partialYear = mod(y,1);
date0 = datenum(num2str(year),'yyyy');
date1 = datenum(num2str(year+1),'yyyy');
daysInYear = date1 - date0;
num = date0 + partialYear .* daysInYear;
end
These three ways have given me an table like this.
[bot_yr bot_mon bot_day]=datevec(bot_date);
bot_V=datevec(bot_date);
datevec(bot_yr);
This way gives me a weird number for the year (54432 instead of 1988). I've tried it several different ways and I keep getting stuck at these two ends, the table and the weird year.

Best Answer

I'd convert your dates to datetimes. Then you can just use the year, month, and day functions.