MATLAB: How to open a part of data in the bin file

binary

Hello. First of all, I do not know about the bin file well.
I have a bin file having the enormous data, all data is number. From that, I just want to import the 7,200 values from the beginning as 80 by 90 array.
So, I make a code is like this.
fileID = fopen('name.bin','w');
Data=fwrite(fileID,[80 90],'double);
fclose(fileID);
But the results are "fileID=1" and "Data=0", fileID is added one as I run the code.
What do these mean? How I can import the data of bin?
please help me.
Thanks for reading my question.

Best Answer

Unfortunately you wrote to the file instead of reading it, and you also completely clobbered the content of the file. I hope you had a backup copy.
You should have used something like,
fileID = fopen('name.bin', 'r');
Data = fread(fileID, [80 90], 'double');
fclose(fileID);
There are some pieces of information you need for binary files:
  1. Is the file written as single precision floating point, or as double precision floating point, or as unsigned 8 bit integers, or as unsigned 16 bit integers, or as signed 16 bit integers? (There are other possibilities for integers, with both signed and unsigned 8, 16, 32, and 64 bit integers)
  2. What is the "byte order" or "endianness" of the binary data. For example, if you had the 16 bit unsigned integer 258 stored in the file, then in hex that could be represented as 0x0102 -- 256^1 * 1 + 256^0 * 2 . That value could be written to the file either in the byte order [01 02] or in the byte order [02 01]. The order [01 02], where the "most significant" byte (the byte that contributes most to the overall value) occurs first in the file representation, is often called "big endian", and the order [02 01], where the "least significant" byte (the byte that contributes least to the overall value) occurs first in the file representation, is often called "little endian". and has a minor (mostly historical) advantage; see https://softwareengineering.stackexchange.com/questions/95556/what-is-the-advantage-of-little-endian-format/95854#95854 . All current systems that MATLAB is implemented on use "little endian" internally, so files written on x86 or x64 systems tend to be in "little endian" order. It is, though, an unnatural order for most cultures in the world, so the more "professional" order is "big endian".
These two considerations can alter the details of reading binary data.