Convert digital numbers to datetime elements
显示 更早的评论
I have two types of time arrays, t1 and t2, with the same number of elements, i.e. 1056, and both indicating a time range of 44 days.
The number 1056 comes from 24 hours (in a day) times 44 days, which represents, indeed, 1056 hours.
The t1 array contains decimal numbers between 0 and 44, while the t2 array contains datetime elements, as shown here below:
% array t1
t1 = linspace(0,44,1057);
>> t1
t1 =
1056×1 single column vector
0
0.04166667
0.08333334
0.125
0.1666667
0.2083333
0.25
0.2916667
0.3333333
...
43.83331
43.87498
43.91665
43.95831
% array t2
a = {'17-Jun-2017'; '30-Jul-2017'};
b = cellstr(datetime(a{1}) : days(1) : datetime(a{end}))
k = 1;
for i = 1:length(b)
for j = 0:23
t2{k} = strjoin([b(i),sprintf('%d:00:00',j)]);
k = k + 1;
end
end
>> t2'
ans =
1056×1 cell array
{'17-Jun-2017 0:00:00' }
{'17-Jun-2017 1:00:00' }
{'17-Jun-2017 2:00:00' }
{'17-Jun-2017 3:00:00' }
{'17-Jun-2017 4:00:00' }
{'17-Jun-2017 5:00:00' }
...
{'30-Jul-2017 19:00:00'}
{'30-Jul-2017 20:00:00'}
{'30-Jul-2017 21:00:00'}
{'30-Jul-2017 22:00:00'}
{'30-Jul-2017 23:00:00'}
Now, if I pick up a random decimal number between 0 and 44, but not necessarily one of those ones contained in the t1 array, how can I convert that decimal number into a datetime element, which would correspond to something between the elements of t2 ?
I know, my question is a bit unclear, but let me give you an example. We have the t1 and t2 arrays:
t1 t2
--------------------------------------------------
0 corresponds to {'17-Jun-2017 0:00:00' }
0.04166667 corresponds to {'17-Jun-2017 1:00:00' }
0.08333334 corresponds to {'17-Jun-2017 2:00:00' }
0.125 corresponds to {'17-Jun-2017 3:00:00' }
... corresponds to ...
--------------------------------------------------
If I pick up
0.1
I guess it will correspond to something similar to:
{'17-Jun-2017 2:30:00' }
Pay attention to the time, which is 2:30 in this example, i.e. between 2:00 and 3:00, both elements of the t2 array.
Therefore, how can I get that
{'17-Jun-2017 2:30:00' }
?
Any magical matlab-trick? :-)
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!