Conversion from unix time string to uint64
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have lidar sensor data and every file is named on its timestamps. The file name 25 characters long (21 characters for time stamps and 4 characters for extension).
For example first file name is "1520944184833390730.pcd"
When I try to convert it to uint64
timeNum = uint64(str2num(fileNames(1:end-4)));
It gives
timeNum = 1520944184833390848
2 commentaires
Jan
le 20 Fév 2019
Modifié(e) : Jan
le 20 Fév 2019
Correctly. What is your question? You cannot store 25 digits exactly in an uint64. 2^64 is about 10^19.26, so you can expect 19 valid digits only. But as long as you do not mention, what your question is, it is not clear, how to help you.
Maybe the first 16 digits are the seconds, and the last 9 the nanoseconds?
Geoff Hayes
le 20 Fév 2019
Modifié(e) : Geoff Hayes
le 20 Fév 2019
Hafiz - what does
str2num(fileNames(1:end-4))
return? Perhaps you are losing some precision with str2num... What happens if you use str2double instead?
In your above example, there are 19 digits in the name (less the extension) but you say The file name 25 characters long (21 characters for time stamps and 4 characters for extension). Which is correct?
Réponse acceptée
Jan
le 21 Fév 2019
The conversion by str2double creates a double, which uses 53 bits only. With sscanf you can get 64 significant bits to store the value:
format long g
a = uint64(str2num('123456789012345678'))
b = sscanf('123456789012345678', '%lu')
>> a = 123456789012345680 % Last 2 digits rounded as expected
>> b = 123456789012345678 % Accurate
With uint64 you can store 19 significant digits: log10(2^64 - 1) = 19.26
You did not mention yet, which problem you want to solve. What do you want to do with the string '1520944184833390730'?
Plus de réponses (2)
Peter Perkins
le 21 Fév 2019
I'm gonna run with Jan's guess, which, in the absence of any description, seems plausible:
>> datetime(1520944184833390730/1e9,'convertFrom','posixtime')
ans =
datetime
13-Mar-2018 12:29:44
Lets say you have that character string.
>> t = '1520944184833390730.pcd';
>> secs = str2num(t(1:10))
secs =
1520944184
>> ns = str2num(t(11:19))
ns =
833390730
>> d = datetime(secs,'ConvertFrom','posixtime','Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS') + milliseconds(ns/1e6)
d =
datetime
13-Mar-2018 12:29:44.833390730
0 commentaires
Hafiz Luqman
le 25 Fév 2019
Modifié(e) : Hafiz Luqman
le 25 Fév 2019
1 commentaire
Jan
le 25 Fév 2019
I'm not sure, what you mean. The current syntax is not valid and uint64('12345') does not create the variable 12345 as UINT64.
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!