how to read .bin file?

165 views (last 30 days)
RAJKUMAR Palaniappan
RAJKUMAR Palaniappan on 14 Jun 2013
I would like to read a .bin file in matlab
Note that all values are 16-bit signed integers stored in binary format (2 bytes – not human readable ASCII text).
Can anyone help me???

Accepted Answer

Walter Roberson
Walter Roberson on 14 Jun 2013
fid = fopen('YourFile.bin', 'r');
data = fread(fid, '*int16');
fclose(fid);
You might need to reshape() the data afterwards.
Depending on how the data was produced and what machine you are using to read it, you might find it necessary to swap the bytes in each integer. There is a MATLAB swapbytes() routine, or there is a FEX contribution http://www.mathworks.com/matlabcentral/fileexchange/17661-swapbytes-c-mex-function that is likely to be faster.
Alternately you might want to add the 'ieee-be' or 'ieee-le' option to the fread() call; that should save you from having to swap the bytes yourself, when the routine that produced the .bin file bothered to document the byte order.
Note: in general, .bin files are not 16 bit signed integers. ".bin" is any arbitrarily complicated binary format, and two .bin files produced by the same program will not necessarily be the same internal format.

More Answers (0)

Categories

Find more on Large Files and Big Data 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!