Getting just the time in Date-time cell

3 次查看(过去 30 天)
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 个)

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 CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by