Reading raw data from binary file
35 次查看(过去 30 天)
显示 更早的评论
I have a binary image file format from a CT scanner. It contains a total of 6 header sections, which are each 512 bytes in length. I know the format of the first header, and can read in all its data with the fread command and different precisions for the different values. I do not, however, know the structure of the 5 consecutive headers, which contain calibration information for the scanner. I would like to be able to copy the data from the last 5 headers in its raw format, and be able to write files with a custom first header, and the 5 successive headers being an exact copy of the data from one of my reference files. As I do not know the format of the last 5 headers, I want to copy this information exactly, with no interpretation of the values. How might I do this? Or am I completely misunderstanding this?
0 个评论
回答(1 个)
Guillaume
2019-1-24
I'm not sure what the problem is. After you've read the first header with your multiple fread just read the bytes of the remaining header, and rewrite them as is later on:
fin = fopen('somefiletoread');
header1.something = fread(fin, ...);
header1.somethingelse = fread(fin, ...);
%... read rest of first header
header2to5 = fread(fid, [1 512*5], '*uint8'); %read 5 times 512 bytes, read as bytes
%...
fout = fopen('newfile', 'w');
%... write header 1
fwrite(fout, header2to5); %rewrite raw data from headers 2 to 5
%...
2 个评论
Guillaume
2019-1-24
There is no interpretation of what the binary values represent. They are read and written back exactly identical. Note that i use '*uint8' to read the values so that they are read and stored as bytes, and thus write them as bytes. If you read them without '*uint8' or with plain 'uint8' (without the *) then the bytes would be stored as double and written back as double.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!