Reading raw data from binary file

34 ビュー (過去 30 日間)
Dylan Zaluski
Dylan Zaluski 2019 年 1 月 24 日
コメント済み: Guillaume 2019 年 1 月 24 日
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?

回答 (1 件)

Guillaume
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 件のコメント
Dylan Zaluski
Dylan Zaluski 2019 年 1 月 24 日
Thanks for the reply Guillaume. The issue is, when I treat the secondary headers as 8-bit integers, and then copy and save them back as that, these values in the headers are misinterpreted by the scanner software reading the files. The values in those fields appear as gibberish (some of them should be characters, some floating point values, but the exact place where each type occurs in the header, I do not know). Therefore I need a way to copy that data from the one file to another, with no interpretation of what the binary values represent.
Guillaume
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 ExchangeFile Operations についてさらに検索

製品


リリース

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by