Convert digital numbers to datetime elements

1 回表示 (過去 30 日間)
Sim
Sim 2022 年 5 月 18 日
コメント済み: Voss 2022 年 5 月 19 日
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? :-)

採用された回答

Voss
Voss 2022 年 5 月 18 日
If you make a datetime array from t2, you can use interp1 to interpolate.
(Also, t1 has 1057 elements, so I'll remove the last one, which corresponds to midnight at the end of 30-Jul-2017, which does not appear in t2.)
% array t1
t1 = linspace(0,44,1057);
% remove the last element of t1:
t1(end) = [];
% 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
% pick 0.1:
t_pick = 0.1;
% interpolate:
interp1(t1(:),datetime(t2(:)),t_pick)
ans = datetime
17-Jun-2017 02:24:00
You say t2 is a datetime array, but actually it's a cell array of character vectors. If you are constructing it yourself as shown, you're probably better off making it a datetime array.
  4 件のコメント
Sim
Sim 2022 年 5 月 19 日
thanks a lot for your comment :)
Voss
Voss 2022 年 5 月 19 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by