Why do the decimals disappear when I put the values into an array?
8 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Jos (10584)
on 2 Oct 2018
Edited: Jos (10584)
on 2 Oct 2018
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)
0 Comments
Guillaume
on 2 Oct 2018
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.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!