Reading raw data from binary file
34 ビュー (過去 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!