MATLAB: Plotting monthly precipitation and time

plottime

Hello There, Essentially I'm trying to plot x (time) and y (precipitation). x = [7/15/05 8/9/05 9/15/05 10/16/05 11/17/05 12/28/05 3/8/06 4/12/06 5/13/06 6/17/06 7/16/06 8/9/06 9/10/06 10/9/06 11/4/06 11/29/06 1/2/07];
y = [2.9 0.51 21.49 25.76 13.16 8.46 11.1 3.38 4.85 14.20 3.05 1.45 19.290 0.20 1.17 0.00 7.67];
The code i'm using is: figure(1) plot(x,y);grid;datetick('x','mm/dd/yyyy')
However my x axis is not plotting on the correct scale (attached figure). I seem to be missing a step. Any suggestions would be helpful! Cheers,

Best Answer

In R2014b or later, use datetimes, not datenums:
>> x = {'7/15/05' '8/9/05' '9/15/05' '10/16/05' '11/17/05' '12/28/05' '3/8/06' '4/12/06' '5/13/06' '6/17/06' '7/16/06' '8/9/06' '9/10/06' '10/9/06' '11/4/06' '11/29/06' '1/2/07'}
x =
1×17 cell array
Columns 1 through 9
'7/15/05' '8/9/05' '9/15/05' '10/16/05' '11/17/05' '12/28/05' '3/8/06' '4/12/06' '5/13/06'
Columns 10 through 17
'6/17/06' '7/16/06' '8/9/06' '9/10/06' '10/9/06' '11/4/06' '11/29/06' '1/2/07'
>> t = datetime(x,'InputFormat','MM/dd/yy')
t =
1×17 datetime array
Columns 1 through 9
15-Jul-2005 09-Aug-2005 15-Sep-2005 16-Oct-2005 17-Nov-2005 28-Dec-2005 08-Mar-2006 12-Apr-2006 13-May-2006
Columns 10 through 17
17-Jun-2006 16-Jul-2006 09-Aug-2006 10-Sep-2006 09-Oct-2006 04-Nov-2006 29-Nov-2006 02-Jan-2007
>> y = [2.9 0.51 21.49 25.76 13.16 8.46 11.1 3.38 4.85 14.20 3.05 1.45 19.290 0.20 1.17 0.00 7.67];
>> plot(t,y)