How to do basic time arithmetic when NaNs are involved

2 ビュー (過去 30 日間)
Blue
Blue 2020 年 5 月 24 日
コメント済み: Blue 2020 年 5 月 24 日
Hi,
I have the following table and I simply want to calculate the elapsed time between start and end points. My main problem is that I dont know how to handle the NaNs. The desired output is elapsed_time (in hours).
start_date = {NaN, '2019-05-21 00:00:00', '2019-05-22 00:00:00', '2019-05-23 00:00:00'}'
start_time = {'13:00', NaN, '01:00', '04:00'}'
end_date = {'2019-05-20 00:00:00', '2019-05-21 00:00:00', NaN, '2019-05-23 00:00:00'}'
end_time = {'14:00', '03:00', NaN, '05:00'}'
T = table(start_date, start_time, end_date, end_time)
% Desired output
T.elapsed_time = {NaT, NaT, NaT, '01:00'}'
Thank you,

回答 (1 件)

Steven Lord
Steven Lord 2020 年 5 月 24 日
Replace the NaN numeric values in your date cell arrays with 'NaT' and the NaN numeric values in your time cell arrays with 'NaN'. When I did this in release R2020a I was able to convert the dates to datetime arrays and the times to duration arrays by calling datetime and duration (with a value for the InputFormat option) respectively. Then just add and subtract them using + and -.
  1 件のコメント
Blue
Blue 2020 年 5 月 24 日
Hi Steven,
Im not too sure how you are doing that replacement bit. Something along the lines of this ?
idx = cellfun(@isnan, T.start_date, 'UniformOutput', false)
T.start_date(idx) = 'Nat'
idx = cellfun(@isnan, T.end_date, 'UniformOutput', false)
T.end_date(idx) = 'Nat'
idx = cellfun(@isnan, T.start_time, 'UniformOutput', false)
T.start_time(idx) = 'NaN'
idx = cellfun(@isnan, T.end_time, 'UniformOutput', false)
T.end_time(idx) = 'NaN'

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

カテゴリ

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