How to import and read a binary file in Matlab

I have a .bin file with data which was saved as '16-bit, Intel format with no header'. Is it possible to import and read and if so how? I have tried without success using fopen and fread.
Thanks a lot for any help!

 Accepted Answer

Jan
Jan on 20 Jul 2017
Edited: Jan on 20 Jul 2017
"16 bit Intel format" is not unique. It could be an UINT16, INT16, FP16. "Intel format" means "little endian". Some ideas:
fileID = fopen(filename, 'r', 'ieee-le');
if fileID == -1, error('Cannot open file: %s', filename); end
format = 'uint16';
Data = fread(fileID, Inf, format);
fclose(fileID);
Then try to set format to 'int16'. You cannot read FP16 directly, but it is not very common also, see https://blogs.mathworks.com/cleve/2017/05/08/half-precision-16-bit-floating-point-arithmetic/.
You have mentioned, that you've "tried without success using fopen and fread". Then post the code and explain the problems. Perhaps there was a typo only.

5 Comments

Thank you, that worked!
Thank you very much, this works!
But there's one thing very interesting:
(The file I want to read is 32 bit)
fid = fopen(filename,'r');
format = 'uint32';
b = fread(fid,Inf,format); % this one works
fclose(fid)
b could read data out
fid = fopen(filename,'r');
c = fread(fid,Inf,'uint32'); % this one doesn't work
fclose(fid)
c could not read data, c is empty
Does anyone know why one cannot specify format in the second way?
The two pieces of code do exactly the same thing. There must be some other difference that you don't show if one work and the other doesn't, so please show the exact code that you've used.
I have a bin file that has values of a signal stored on my laptop but i can't seem to open it using this code.Should i give the address of the file instead of filename?I treid that and that didn't work.
What should i do?
@Niloufar, please start your own question as it's unlikely that the answer here applies to your file.
In your question, give more details about your binary file. Do you know how the signals are encoded in the file?
Also, if you get an error, show us the exact you're using and the complete text of the error message.

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 20 Jul 2017

Commented:

on 11 Mar 2020

Community Treasure Hunt

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

Start Hunting!