Getting just the time in Date-time cell

2 vues (au cours des 30 derniers jours)
Yannick Tabot Njami
Yannick Tabot Njami le 28 Oct 2022
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

Réponse acceptée

Florian Bidaud
Florian Bidaud le 28 Oct 2022
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 commentaire
Yannick Tabot Njami
Yannick Tabot Njami le 28 Oct 2022
thank you very much for the help :)

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 28 Oct 2022
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"

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by