>> dt=datetime(1991,1+[0:321].',1);
>> whos dt
Name Size Bytes Class Attributes
dt 322x1 5153 datetime
>> [dt(1) dt(end)]
ans =
1×2 datetime array
01-Jan-1991 01-Oct-2017
>>
Once you have that, then simply plot your data against them...
It is much simpler in Matlab if you do NOT create sequentially-named/numbered variables but use array syntax and subscripting to refer to individual columns (variables) within.
With the above, ML will automatically place ticks on a scale that will fit; probably only annually given the amount of data you have and the room required for date ticks to be drawn. You can change these arbitrarily with the DatetimeRuler properties;
>> hAx=gca;
>> hAx.XAxis.TickLabelFormat
ans =
'u'
>> hAx.XAxis.TickLabelFormat='MMMyy';
You can set the number and spacing of ticks as please, with using datetime you can do this with direct datetime values and not have to mess with datetick at all; MUCH more convenient!
>> xlim
ans =
1×2 datetime array
Jan 01, 1991 Oct 01, 2017
>>
To set six-months ticks, just select every sixth time value that you already have--
Those are undoubtedly going to overlap; try to see if can fit...
hAx.XTickLabelRotation=30;
hAx.FontSize=8;
Salt to suit from these to get what want...
Best Answer