MATLAB: Create a monthly datetime sequence

calmonthsdatetimedatevecMATLAB and Simulink Student Suite

Hello everyone,
Can someone please me explain why this code
a=datetime(1981,1,1);
b=datetime(2019,31,1);
t1=a:calmonths(1):b;
t2=datetime(t1,'Format','yyyy-M-d');
M=datevec(t2);
Gives me monthly dates until july 2021? Am I missing something?
Thank you!

Best Answer

a=datetime(1981,1,1);
b=datetime(2019,1,31);%must be year,month,day
t1=a:calmonths(1):b;
t2=datetime(t1,'Format','yyyy-M-d');
M=datevec(t2);