我有问题在Matlab中将datenum转换为datetime。
给定dnum = [floor(now); floor(now+1)];
我尝试了以下内容:
datenum(dnum)
但那没用。
我发现工作的方式是:
datetime(datestr(dnum)) % seems slow?
datetime(year(dnum), month(dnum), day(dnum)) % slower?
最快的方法是什么?有更好的方法吗?内置功能?
答案 0 :(得分:1)
在尝试了几个建议之后,这就是细分:
dnum = datenum(datetime(1900,1,1):datetime(2017,1,1))';
然后是结果(Matlab 2016b):
% Elapsed time is 1.287081 seconds.
tic
dtime = datetime(datestr(dnum)); % seems slow?
toc
% Elapsed time is 0.017474 seconds.
tic
dtime = datetime(year(dnum), month(dnum), day(dnum));
toc
% Elapsed time is 0.010327 seconds.
tic
dtime = datetime(datevec(dnum));
toc
% Elapsed time is 0.000949 seconds.
tic
dtime = datetime(dnum,'ConvertFrom','datenum');
toc
以下是复制/粘贴的功能:
function dtime = datenum2datetime(dnum)
dtime = datetime(dnum,'ConvertFrom','datenum');
end