Plotting of dates error

1 view (last 30 days)
Jim O'Doherty
Jim O'Doherty on 17 Oct 2011
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

Accepted Answer

Daniel Shub
Daniel Shub on 17 Oct 2011
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 Comments
Jim O'Doherty
Jim O'Doherty on 17 Oct 2011
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 on 17 Oct 2011
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!