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')

 채택된 답변

Adam
Adam 2017년 1월 11일
편집: Adam 2017년 1월 11일

1 개 추천

'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

Yumi Lee
Yumi Lee 2017년 1월 11일
Thank you so much, you SAVED my life :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Peter Perkins
Peter Perkins 2017년 1월 12일

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)

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2017년 1월 11일

답변:

2017년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by