Plotting of dates error

조회 수: 1 (최근 30일)
Jim O'Doherty
Jim O'Doherty 2011년 10월 17일
Dear all, Should be an easy question but I'm having much difficuly plotting non-monotically increasing dates with my data. Heres a simplified version of my code:
x= ['20 Sep 2011'
'21 Sep 2011'
'21 Sep 2011'
'23 Sep 2011'
'27 Sep 2011'
'28 Sep 2011'
'29 Sep 2011'
'1 Oct 2011'];
y = [ 30255600 30248900 30252600 30281600 30223900 30229100 30336700 30270800 ];
dates = datenum(x, 'dd mmm yyyy'); plot(dates, y, '.-y')
labels = datestr(dates, 20); set(gca, 'XTick', dates); set(gca, 'XTickLabel', labels);
I get an error: ??? Error using ==> set Values must be monotonically increasing
Error in ==> ecf_read at 127 set(gca, 'XTick', dates);
Can anyone help?
Regards, Jim

채택된 답변

Daniel Shub
Daniel Shub 2011년 10월 17일
While there seems to be at least two bugs ...
The data 21 Sep 2011 is listed twice. The error probably should be "Values must be STRICTLY monotonically increasing." You need to do something to get rid of the identical dates in the XTick call. You could do something like unique.
doc unique
The second problem is that 1 Oct 2011 has less characters than the other dates. You cannot do concatenation like that. You need a cell array.
  댓글 수: 2
Jim O'Doherty
Jim O'Doherty 2011년 10월 17일
Hello,
Thanks for the reply. I'm actually using cell arrays. my original date (x data) was in a cell array before I converted it via datenum. Also my y data was a spearate cell array which I converted because the datenum array was a double.
As for the duplication of dates, that was a typo, it should have been 22...
And as for the "1 Oct" part, the data comes from elsewhere so I'm not sure how to pad it with a zero. Would the original cell array solve the problem?
Daniel Shub
Daniel Shub 2011년 10월 17일
Then post some minimal code that reproduces the error. Yes, a cell array will allow you to have dates with different numbers of characters. The problem still remains that something about dates is not monotonically increasing. I would suggest using unique and sort on dates after the plotting, but before the setting of the ticks and the labeling.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by