Plotting a graph of a date !!

I am trying to plot a graph of the amount of rain over one year ( 12 months) using the code below, but the problem is that only 3 months appear on the graph ( Jan, Feb and March) and I have no I idea where I went wrong !!
startDate = datenum('Oct 1991');
endDate = datenum('Sep 1992');
xData = linspace(startDate,endDate,12);
plot(xData,Max_rain)
datetick('x','mmm')

 Accepted Answer

Adam
Adam on 11 Jan 2017
Edited: Adam on 11 Jan 2017
'Oct 1991' and 'Sep 1992'
are not a valid format for a date string so these get interpreted totally differently to what you expect.
doc datenum
has a big section on the valid formats for DateString. e.g.
startDate = datenum('1-Oct-1991');
So far as I can see you cannot have a date that does not include a day so I guess you can just pick whatever day suits you for each month.
You can do something like this:
startDate = datenum('Oct/1991', 'mmm/yyyy' )
but
datestr( startDate )
will still default to:
01-Oct-1991
because to create the date it also specifies a number.

1 Comment

Thank you so much, you SAVED my life :)

Sign in to comment.

More Answers (1)

In recent versions of MATLAB (since R2014b), do this:
>> t = datetime(1991,10,1) + calmonths(0:11)
t =
1×12 datetime array
Columns 1 through 9
01-Oct-1991 01-Nov-1991 01-Dec-1991 01-Jan-1992 01-Feb-1992 01-Mar-1992 01-Apr-1992 01-May-1992 01-Jun-1992
Columns 10 through 12
01-Jul-1992 01-Aug-1992 01-Sep-1992
>> x = 1:12;
>> plot(t,x)

Categories

Asked:

on 11 Jan 2017

Answered:

on 12 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!