Not able to convert string to seconds! Even though I am using datenum function. How can it be solved?

I have been trying to convert time string from a large datal, but it Matlab gives me "Unrecognized millisecond format. Format string: HH:MM:SS.FFFF."
And I would like to get the data immediately from the file instead of importing them.
The format of the string is
12:10:02.908990
Here is the code:
t1;
datenum(t1,'HH:MM:SS.FFFF')

Respuestas (2)

in MATLAB R202a and later you can use
datetime command like this ...
t1 = '12:10:02.908990'
t1 = '12:10:02.908990'
datetime(t1)
ans = datetime
30-Jul-2023 12:10:02
datenum() only supports up to millisecond resolution -- so at most FFF .
Also, your input format needs to account for the entire input string. You cannot just specify FFF and expect it to stop parsing after it reads 3 digits of milliseconds.
t1 = '12:10:02.908990';
d = duration(t1, 'InputFormat', 'hh:mm:ss.SSSSSS', 'format','hh:mm:ss.SSSSSS')
d = duration
12:10:02.908990
datenum_you_were_looking_for = seconds(d) / (24 * 60 * 60)
datenum_you_were_looking_for = 0.5070
ds = datestr(datenum_you_were_looking_for, 31) %cross-checking
ds = '0000-01-00 12:10:02'
I don't think the number of days since the beginning of "0 AD" is going to be useful, especially with the lost resolution (because datenum just do not have the resolution)- You should probably be seriously considering getting rid of using datenum()

3 comentarios

What are other alternatives to datenum()?
I have tried to use multiple functions to convert strings like '12:10:02.908990' to seconds, but I do not get them correctly.
I have a large amount of data, i.e., 300k samples, and I want to get the time in seconds that is associated with each sample to be able to analyze the data.
When I tried to import the data and then convert them in the import data to (HH:MM:SS), most of the time was lost due to the poor resolution.
If anyone has come across such an issue, please let us know how to figure it out.
"What are other alternatives to datenum()? "
Lets read the DATENUM documentation and see what it tells us:
The DURATION class would suit your input data (times given as hours, minutes, seconds + fraction of second). Note the DURATION class has properties and methods that you will find useful.
datetime and duration classes have the resolution needed, but their default Format does not include full resolution for display purposes.

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 30 de Jul. de 2023

Comentada:

el 30 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by