Decode binary file with a given data frame
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dear community, I am facing a lot of troubles as I am very inexperienced reading binary files. I want to decode a binary file and I know exactly the data frame.

So, in order to decode the header, I know that its size is 128 bytes. How can I proceed to retrieve each field given in the table?
0 Kommentare
Antworten (1)
Pratyush Swain
am 27 Nov. 2023
Hi César,
I understand you are facing problem in reading specific fields of binary files and you have prior knowledge regarding the dataframes existing in it such as size in bytes, offset and data type.
In this case, the 'fread' function and 'fseek' function together can help to decode the binary file.Please refer to the example implementation below:
% Writing a binary file with uint16 and int64 values
fileID = fopen('nine.bin','w');
fwrite(fileID,[1:9],'uint16');
fclose(fileID);
% Open the file in append mode
fileID = fopen('nine.bin','a');
data = int64(10:18);
% Write int64 data to the end of the file
fwrite(fileID, data, 'int64');
% Close the file
fclose(fileID);
Now, lets try to read the different data type values.
fopen('nine.bin','r');
% Read uint16 values in the form of a matrix
values_uint16 = fread(fileID,[3,3],'uint16')
% We can also read the values in form of a column
values_uint16 = fread(fileID,9,'uint16')
Please note the 'uint16' data type is of 2 bytes and we have 9 values in the beginning of file, so offset value becomes 9x2 = 18 bytes in order to access the next set of values.
% Using 'fseek' function to reach specific point in file
fseek(fileID,18,'bof'); % 'bof' signifies beginning of file
% Reading 'int64' data values
values_uint16 = fread(fileID,9,'int64')
We can perform similar type of operations for a file having variety of fields with different data types. For more information please refer to:
Hope this helps
0 Kommentare
Siehe auch
Kategorien
Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!