生成一致的datenums数组 [英] Generating consistent arrays of datenums
问题描述
每当需要绘制数据与时间的关系时,都会生成相应的datenums
数组,这样我就可以通过调用datetick
来可视化plot
上的时间轴.
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
.
假设我需要所有在h_1
和h_2
小时之间有1分钟间隔的datenum.这就是我如何生成我的datenums vd
数组:
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;
我阅读了这个答案.
另一方面,每当我希望生成单个datenum时,我都会使用datenum
函数,如下所示:
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');
由于时间间隔01:00-17:30之间包含14:20,因此它的datenum也应该是如此!不幸的是,这不能按预期方式工作,因为分配给d
的数字与vd
中包含的值根本不同.我想我可能会遗漏一些东西,可能与设置参考日期或类似日期有关.
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.
那么一致地生成datenum的合适方法是什么?
So what would be the appropriate way to generate datenums consistently?
推荐答案
原因是datenum函数为您提供了从0000年1月0日开始的天数. 所以在打电话时
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');
您得到的数字约为735965,而vd数组中的数字介于0和1之间.为了构造1月0日,0000年至今的日期,您可以编写
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));
结果:
d = 0.5972
ans = 0.5972
这篇关于生成一致的datenums数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!