serenomateus
serenomateus

Reputation: 26

Unexpected behavior of datetick function in Octave plotting

I'm trying to plot a graphic that displays values against moments of time. For this, I have an array of time instants (in Epoch) and an array of values.

I've already been able to plot the graphic normally using the raw time (as Epoch). The problem is specifically in the conversion of the axis time format.

hold on;
plot(horizontal, pre_X(:,4), 'b-');
xt = get(gca, 'xtick'); 
set(gca, 'xticklabel', sprintf('%d|', xt));
datetick ("x", "dd/mmm/YY HH:MM");
yt = get(gca, 'ytick'); 
set(gca, 'yticklabel', sprintf('%d|', yt));
hold off;

The datetick function was supposed to be able to transform these Epoch times into nicely formatted ones, but I am not getting the expected result. Instead, all time instants get labeled as the same (01/JAN/00 00:00) which is weird.

The plot without the

datetick ("x", "dd/mmm/YY HH:MM");

line works fine, but gives the time information in Epoch, which is not what I intend to.

Any help would be appreciated!

NOTE: If the right function to do what I intend to turns out not to be "datetick", also please let me know! All I need is to get the X axis to be formatted nicely into readable time.

EDIT: By Epoch Time, I mean Unix Time.

Upvotes: 1

Views: 483

Answers (1)

Nick J
Nick J

Reputation: 1630

Lacking more info, I'm going to assume that by Epoch time you mean posix, or Unix time

I would expect that to then be represented as a 32-bit integer, and should be the number of seconds counted from 'zero time' (as described in the Wiki linked) (it may also be a floating point number including fractional seconds using the same scale).

According to the Matlab help for datetick, it expects the axis data to be "serial date numbers, as returned by the datenum function". For compatibility, octave likely expects the same, although the datetick function reference does not state such explicitly.

The datenum "serial date number" format is another serial representation of time, but it has a different scale and reference than Epoch/posix/unix time. According to the datenum function description, it's serial definition is "date/time input as a serial day number, with Jan 1, 0000 defined as day 1".

That's a long way of saying you're time is probably in units of seconds, whereas datenum expects units of days.

Now, you can probably address this a couple ways. you can covert all of your times to the datenum scale before plotting. from this example, something like this would work in Matlab:

datetime(1470144960, 'convertfrom','posixtime')

According to bug #47032 datetime has not yet been implemented in Octave, but that bug report does link to a github repository with a datetime class implementation (under the inst/ folder).

To manually convert from unix to matlab time, you could convert following this example from the Mathworks File exchange:

unix_epoch = datenum(1970,1,1,0,0,0);
matlab_time = unix_time./86400 + unix_epoch; 

(assuming your x-axis data is the unix_time variable)

Once you get your data into the 'datenum' scale, datetick should perform correctly.

Upvotes: 1

Related Questions