MATLAB: Fread: how to read n bytes from bin files

fread binary file

Hi! I have a binary file. It consists of records: 8 byte long, 4 byte float, 4 byte float, 4 byte float. I want to read the file into array Nx4. N – records number. How can I do it? I tried fread but without result.

Best Answer

Is it correct? The easiest way for you to find out is to try.
fid = fopen('acc-angle.bin', 'rb')
There's no 'b' option for fopen permissions. 'r' is all that is needed for binary.
Otherwise, your code looks ok to me as long as your file has little endian encoding. A faster option would be to read all records at once using the skip parameter of fread, something like:
fid = fopen('acc-angle.bin', 'r');
time = fread(fid, Inf, 'int64', 12);
fseek(fid, 'bof', 8);
ax = fread(fid, Inf, 'single', 16);
fseek(fid, 'bof', 12);
ay = fread(fid, Inf, 'single', 16);
fseek(fid, 'bof', 16);
az = fread(fid, Inf, 'single', 16);
fclose(fid);