Getting just the time in Date-time cell

2 ビュー (過去 30 日間)
Yannick Tabot Njami
Yannick Tabot Njami 2022 年 10 月 28 日
回答済み: Steven Lord 2022 年 10 月 28 日
Hello Please can someone help me get just the time in this date-time cell array?
I will appreciate the efforts.
attached is the data.
the first 10 rows looks like this and i want a new row with just the time such [12:33:00 12:34:00 12:35:00] and so on
'16.09.2020 12:33:00'
'16.09.2020 12:34:00'
'16.09.2020 12:35:00'
'16.09.2020 12:36:00'
'16.09.2020 12:37:00'
'16.09.2020 12:38:00'
'16.09.2020 12:39:00'
'16.09.2020 12:40:00'
'16.09.2020 12:41:00'
'16.09.2020 12:42:00'
thanks

採用された回答

Florian Bidaud
Florian Bidaud 2022 年 10 月 28 日
Hi
for i = 1:length(txt(:,1))
if ~isempty()
txtSplit = strsplit(txt{i,1},' ');
txt{i,2} = txtSplit{2};
end
The value when the time is 00:00 is empty, so you will have to add the following check :
for i = 1:length(txt(:,1))
txtSplit = strsplit(txt{i,1},' ');
if length(txtSplit) == 1
txt{i,2} = '00:00:00';
else
txt{i,2} = txtSplit{2};
end
end
  1 件のコメント
Yannick Tabot Njami
Yannick Tabot Njami 2022 年 10 月 28 日
thank you very much for the help :)

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 10 月 28 日
I would turn the text representation of the dates and times into a datetime array then call the timeofday function on that array. If you need the representations as a string you can call string on the duration array created by timeofday.
s1 = {'16.09.2020 12:33:00'
'16.09.2020 12:34:00'
'16.09.2020 12:35:00'
'16.09.2020 12:36:00'
'16.09.2020 12:37:00'
'16.09.2020 12:38:00'
'16.09.2020 12:39:00'
'16.09.2020 12:40:00'
'16.09.2020 12:41:00'
'16.09.2020 12:42:00'};
dt = datetime(s1)
dt = 10×1 datetime array
16-Sep-2020 12:33:00 16-Sep-2020 12:34:00 16-Sep-2020 12:35:00 16-Sep-2020 12:36:00 16-Sep-2020 12:37:00 16-Sep-2020 12:38:00 16-Sep-2020 12:39:00 16-Sep-2020 12:40:00 16-Sep-2020 12:41:00 16-Sep-2020 12:42:00
du = timeofday(dt)
du = 10×1 duration array
12:33:00 12:34:00 12:35:00 12:36:00 12:37:00 12:38:00 12:39:00 12:40:00 12:41:00 12:42:00
s2 = string(du)
s2 = 10×1 string array
"12:33:00" "12:34:00" "12:35:00" "12:36:00" "12:37:00" "12:38:00" "12:39:00" "12:40:00" "12:41:00" "12:42:00"

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by