How do I convert a specific string to a date ?

Hi everyone,
I have acceleration measurements that were collected via a tag.
The time is recorded as '22-07-04T013:09:42Z' (it is exactly written like that in my data).
Do you know how I can convert this type of string into a date? And if it possible, select only the time which here would be '13:09:42' ?
I tried datetime and datenum functions but I guess the specific format makes it more complicated.
Thanks,
Marine

2 commentaires

Stephen23
Stephen23 le 29 Juil 2022
"The time is recorded as '22-07-04T013:09:42Z' (it is exactly written like that in my data)."
Please check your data: it is odd to use three digits for the hours. Otherwise, it looks like a normal ISO 8601 timestamp.
Marpe
Marpe le 29 Juil 2022
Yes I know it is odd but I can't do anything about it unfortunately…

Connectez-vous pour commenter.

 Réponse acceptée

Note the Comment by @Stephen23.
That aside, try this—
DT = datetime('22-07-04T013:09:42Z', 'InputFormat','dd-MM-yy''T''HHH:mm:ss''Z''', 'TimeZone','UTC') % Convert To 'datetime'
DT = datetime
22-Jul-2004 13:09:42
DT.Format = 'HH:mm:ss' % Select Only Time Time
DT = datetime
13:09:42
Here, datetime does not have problems with three-digit hours, providing that the first digit is a 0. I have no idea what it would do otherwise.
.

4 commentaires

Thanks all, everything is working well but I don't know how to do that for my entire data. I tried just by replacing with my table but I have the error message just below. I can't figure out in which format this time data is…
DT = datetime(T, 'InputFormat','dd-MM-yy''T''HHH:mm:ss''Z''', 'TimeZone','UTC') % Convert To 'datetime'
Error using datetime
Input data must be a numeric array, a string array, a cell array containing character
vectors, or a char matrix.
DT.Format = 'HH:mm:ss' % Select Only Time Time
" tried just by replacing with my table but I have the error message just below."
You need to select the particular table variable/column by its name (which you have not told us):
DT = datetime(T.nameOfTheVariable,..)
Marpe
Marpe le 29 Juil 2022
Thank you a lot. I'm still trying to figure out so much "simple" thing in Matlab…
Star Strider
Star Strider le 29 Juil 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
@Stephen23 — Thank you!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by