Reading raw data from binary file

35 次查看(过去 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 CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by