フィルターのクリア

Conversion from unix time string to uint64

18 ビュー (過去 30 日間)
Hafiz Luqman
Hafiz Luqman 2019 年 2 月 20 日
コメント済み: Jan 2019 年 2 月 25 日
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 件のコメント
Jan
Jan 2019 年 2 月 20 日
編集済み: Jan 2019 年 2 月 20 日
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
Geoff Hayes 2019 年 2 月 20 日
編集済み: Geoff Hayes 2019 年 2 月 20 日
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?

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 2 月 21 日
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'?
  1 件のコメント
Hafiz Luqman
Hafiz Luqman 2019 年 2 月 25 日
Thanks for helping :)

サインインしてコメントする。

その他の回答 (2 件)

Peter Perkins
Peter Perkins 2019 年 2 月 21 日
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

Hafiz Luqman
Hafiz Luqman 2019 年 2 月 25 日
編集済み: Hafiz Luqman 2019 年 2 月 25 日
Jan method works fine. I also figured it out by this way.
a = str2num(['uint64('123456789012345678');
  1 件のコメント
Jan
Jan 2019 年 2 月 25 日
I'm not sure, what you mean. The current syntax is not valid and uint64('12345') does not create the variable 12345 as UINT64.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by