How to save signal as double in .dat file?

9 views (last 30 days)
Christopher
Christopher on 29 Nov 2014
Edited: Christopher on 29 Nov 2014
I need to save a signal generated in Matlab in a .dat file format as double precision floating point numbers. The purpose is so this file can be read from an executable I have written to transmit this data using a Software Defined Radio.
I can't seam to find anything online. Most commands that save the data seem to always save it in ASCII.
Thank you for the help

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 29 Nov 2014
Use fwrite
  2 Comments
Christopher
Christopher on 29 Nov 2014
Thank you Aziz, I have tried this and wanted to verify that it worked as expected. When I try to recover the data I had saved using fread, I just get an empty matrix. For example after the file is created and saved I try to recover the data using B = fread(fid,N,'float'); Where N is the number of elements I wish to use, then I just get B = []. Am I just doing the fread incorrectly? If I don't specify a precession it reads ever number rounded to the nearest bit, i.e. reads 0 or 1 depending on whatever the number was closest to.
Christopher
Christopher on 29 Nov 2014
Ignore my last comment. I solved the problem. Thank you.

Sign in to comment.


Christopher
Christopher on 29 Nov 2014
Edited: Christopher on 29 Nov 2014
Problem solved. For anyone who may also be looking for an answer to this question this is exactly what I did:
fid = fopen('samples','w'); % create file to write to
A = [0.03 -0.67 0.789] % arbitrary floating point numbers to test
fwrite(fid,A,'float'); % Write to file as floating point numbers
fid = fopen('samples','r'); % open file for readin
B = fread(fid,3,'float') % Read the floating point number
Sometimes I know I go looking for answers, and someone responds that they solved the problem but don't state how, so I left this for the next guy who runs into this problem, and comes looking for a concise answer.

Community Treasure Hunt

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

Start Hunting!