Creating consistent datasets
Whenever I need to plot data over time, I generate an appropriate array datenums
, so I can render the timeline on plot
by calling datetick
.
Suppose I want all the data with a 1 minute interval between hours h_1
and h_2
. This is how I can generate an array of arrays of data 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 found out that this method reads this answer .
On the other hand, whenever I want to create a single number, I use the function datenum
like this:
d = datenum('14:20','HH:MM');
Since 14:20 is between 01:00 - 17:30, its date must also be! Unfortunately, this does not work as expected, as the number assigned d
is radically different from the values contained in vd
. I think I might be missing something, perhaps something related to setting a key date or similar.
So what would be a suitable way to generate columns sequentially?
source to share
The reason is that the datacenter function gives you a few days from January 0, 0000. Therefore, when calling
d = datenum('14:20','HH:MM');
you will get a number around 735965 and the numbers in your vd array are between 0 and 1. To adjust the days between January 0 and 0000, 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
source to share