# Generating consistent arrays of datenums

Whenever I need to plot data vs time I generate the corresponding array of datenums so I can visualize a timeline on the plot by calling datetick.

Let's suppose I need all the datenums with a 1 minute interval between hours h_1 and h_2. This is how would I generate my array of datenums vd:

h_1 = [01 00]; % 01:00 AM h_2 = [17 00]; % 17:00 PM HH_1 = h_1(1); % Hour digits of h_1 MM_1 = h_1(2); % Minute digits of h_1 HH_2 = h_2(1); % Hour digits of h_2 MM_2 = h_2(2); % Minute digits of h_2 % Vector of 01:00 - 17:30 with 1 minute increments (24 hours a day, 1440 minutes a day) vd = HH_1/24+MM_1/1440:1/1440:HH_2/24+MM_2/1440;

I learnt this technique reading this answer.

On the other hand, whenever I wish to generate a single datenum, I use the datenum function like this:

d = datenum('14:20','HH:MM');

Since 14:20 is comprised between the interval 01:00 - 17:30, its datenum should be too! Unfortunately this is not working as expected, since the number assigned to d is radically different to those values contained in vd. I think I might be missing something, probably something to do with setting up a reference date or similar.

So what would be the appropriate way to generate datenums consistently?

## Answers

The reason is that the datenum function gives you a number of days from January 0, 0000. So when calling

d = datenum('14:20','HH:MM');

you get a number about 735965, while the numbers in your vd array are between 0 and 1. In order to substruct the days between January 0, 0000 and today you can write

d = datenum('14:20','HH:MM') - datenum('00:00','HH:MM');

Then your code will look like

h_1 = [01 00]; % 01:00 AM h_2 = [17 00]; % 17:00 PM HH_1 = h_1(1); % Hour digits of h_1 MM_1 = h_1(2); % Minute digits of h_1 HH_2 = h_2(1); % Hour digits of h_2 MM_2 = h_2(2); % Minute digits of h_2 vd = HH_1/24 + MM_1/1440 : 1/1440 : HH_2/24+MM_2/1440; d = datenum('14:20','HH:MM') - datenum('00:00','HH:MM'); display(d); display(vd(801));

And the result:

d = 0.5972 ans = 0.5972