You got rounded number because you are writing and reading using long
, which is a 32 bit integer. To make it work, you need replace long
by floating types like double
, float
etc.
number=-3.913;
fid_out=fopen('test','wb');
fwrite(fid_out,number,'double');
fclose all;
fid_in=fopen('test','rb');
fread(fid_in,'double')
You will get:
ans =
-3.9130
Edit: If you have to use long
, you can first multiple the numbers by a big scale number like 1000
to make it a long
and at the same time don't lose the accuracy. Then when loading the data back, you can get the original values by further dividing them by the same scale.