Why do the decimals disappear when I put the values into an array?

I am streaming data from a tracking device and I want to put each TimeStamp datapoint into an array by a for loop so that each run in the loop adds a TimeStamp line in the matrix, the problem is that the decimals disappear and are rounded to 0 or 1 when I add them to the matrix.
TimeStamp = 102545096954
Posx = 0.3265729844570160
Posy = 0.5914849996566772
for i=1:size(result,1)
value = [value ; TimeStamp(i) Posx(i) Posy(i)]
end
Gives
value =
501x3 uint64 matrix
102545096954 0 1
102545096955 0 1
102545096956 1 0
etc...
What am I doing wrong here?

답변 (2개)

Jos (10584)
Jos (10584) 2018년 10월 2일
편집: Jos (10584) 2018년 10월 2일
You have somehow initialised the array value as a uint64. When you add a row, this new row is hence converted to uint64 as well.
You could explicitly tell matlab to convert the array to double, just before the loop:
value = double(value)
Guillaume
Guillaume 2018년 10월 2일
I would suspect the problem is not with the initialisation of value but with the fact that TimeStamp is of class uint64. If you combine doubles with integer types, everything gets converted to integer ( the rules are even more complicated if you combine different integer types).
So,
value = [value; double(TimeStamp(i)), Posx(i), Posy(i)]
should solve your problem. Note however that if Timestamp is greater than flintmax the value will be rounded when converted to double as not all 64 bit integers above this value can be stored exactly as double.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2018년 10월 2일

답변:

2018년 10월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by