MATLAB: How to read .bin file

.binfilesMATLABread

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???

Best Answer

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.
Related Question