derhelge
derhelge

Reputation: 93

Unexpected behavior of datetick in octave

The following code should display data from startDate=datenum('2016-01-01'); to endDate=datenum('2016-12-31');:

data=[ 7.3633e+05,     3460.4;
  7.3635e+05,     3119.7;
   7.364e+05,     2777.9;
  7.3642e+05,     2202.6;
  7.3649e+05,     569.57;
  7.3656e+05,      403.1;
  7.3658e+05,      789.5;
  7.3664e+05,     2242.1;
  7.3668e+05,     3120.6 ]

startDate=datenum('2016-01-01');
endDate=datenum('2016-12-31');

xData = linspace(startDate,endDate,12)
plot(data(:,1),data(:,2));

ax = gca;
set (ax, 'xtick',xData);
datetick (29, 'x');

but the datetick seems to ignore the values in xData as can be seen in the figure below. In my opinion it should display dates in the range 2016-01-01 to 2016-12-31. datetick problem

Upvotes: 2

Views: 783

Answers (1)

Suever
Suever

Reputation: 65460

The issue is that you've switched the first two inputs of datetick. As you have it written, Octave will read the first input, 29, and use this as the format (as you expect); however, any trailing inputs after the format specification are used to indicate the start date to use (rather than relying on the tick values themselves). This seems to be intentionally undocumented behavior.

datestr(double('x'), 29)
%   0000-04-29

To get the expected behavior, you need to flip the order of the two inputs as the the axis specification (the 'x') should come first.

datetick('x', 29)

enter image description here

Also, if you want to specify exactly where the xticks are going to be, you'll want to use the "keepticks" option.

datetick('x', 29, 'keepticks')

Upvotes: 2

Related Questions