How do to get a specific time from datetime format in Matlab?

5 ビュー (過去 30 日間)
Kofial
Kofial 2020 年 11 月 11 日
コメント済み: Kofial 2020 年 11 月 12 日
I have these data:
x=(17-Apr-2020 06:59:00,17-Apr-2020 07:00:00,17-Apr-2020 07:01:00,17-Apr-2020,07:02:0017-Apr-2020,07:03:00)
y=(06:58:30,17-Apr-2020 06:59:30,17-Apr-2020 07:00:30,17-Apr-2020 07:01:30,17-Apr-2020 07:02:30,17-Apr-2020 07:03:30)
Both times (x,y) are in the datetime format).
To get the time for x from 07:00:00 till the end I do this:
interpolation_time = (x(420):minutes(1):x(end));
For y I need it from 07:00:30 till the end. How can I do it? It has a different timestamp.
Thank you!
  1 件のコメント
Kofial
Kofial 2020 年 11 月 12 日
It works very well. Thank you

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 11 月 11 日
編集済み: Walter Roberson 2020 年 11 月 11 日
[xh, xm, xs] = hms(x);
xidx = find(xh >= 7 & xm >= 0, 1);
[yh, ym, ys] = hms(y);
yidx = find(yh >= 7 & ym >= 0 & ys >= 30, 1);
selected_x = x(xidx:end);
selected_y = y(yidx:end);
Note here that this code is perfectly happy if the dates do not match at all, and if the entries are out of order. Your problem definition involved finding the first entry that meets a particular time-based criteria that ignores dates.
Perhaps you should use isbetween() ?
See also dateshift() -- for example
xbase = dateshift(x(1), 'start', 'day');
dx = x - xbase;
dy = y - xbase;
maskx = dx >= hours(7);
masky = dy >= hours(7) + seconds(30);
  1 件のコメント
Steven Lord
Steven Lord 2020 年 11 月 11 日
You can compute dx and dy without computing xbase using the timeofday function for datetime arrays.
dt = datetime('now') + hours(48*randn(10, 1)) + minutes(60*randn(10, 1));
td = timeofday(dt);
results = table(dt, td, 'VariableNames', ["Date and time", "Time since midnight"])
results = 10x2 table
Date and time time since midnight ____________________ ___________________ 09-Nov-2020 07:06:30 07:06:30 10-Nov-2020 16:46:13 16:46:13 09-Nov-2020 20:24:50 20:24:50 12-Nov-2020 20:58:31 20:58:31 10-Nov-2020 09:09:45 09:09:45 09-Nov-2020 02:58:16 02:58:16 13-Nov-2020 12:14:36 12:14:36 11-Nov-2020 13:15:02 13:15:02 12-Nov-2020 01:21:48 01:21:48 09-Nov-2020 12:10:02 12:10:02

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by