1

I have a problem saving a double vector (signal) to a binary file with single precision in R. I have used R function writeBin() to save a signal in .sng file, but it is not saving correctly.

writeBin(as.vector(signal),fileID)

Previously, I have used MATLAB and function fwrite(fileID, signal, 'single') and was working perfectly. Now, I am looking to similar R function, analogue to fwrite().

Any help ?

Best, S

Ric S
  • 9,073
  • 3
  • 25
  • 51
stevanche
  • 21
  • 3
  • Please take a look at [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In this case it is necessary to give an example of your expected output and show what's incorrect. – Martin Gal May 27 '20 at 08:17
  • 1
    For those unfamiliar with `matlab`, it would be courteous of you to offer an explanation of what its `fwrite` function does. Then please explain why the data you saved to a `.sng` file is not correct, and how it should be properly formatted. It would also help to have sample data we can play with to be able to test saving it to your expected output. – r2evans May 27 '20 at 08:28
  • 1
    Use the `size` and maybe `endian` arguments to `writeBin`. The default size for numerics corresponds to double precision, not single precision. – user2554330 May 27 '20 at 11:16

1 Answers1

0

The answer of my question is:

writeBin(as.vector(signal),fileID, size=4)

stevanche
  • 21
  • 3