How to open binary file where each number has different precision?

6 ビュー (過去 30 日間)
Giulia Pazzi
Giulia Pazzi 2017 年 4 月 14 日
回答済み: Image Analyst 2017 年 4 月 17 日
I have to open a binary file and I have to order it in a matrix of 3 columns where in the 1st column there are integers and in the 2nd and the 3rd long double. With freed I got an [m,3] matrix of integers.
  1 件のコメント
Jan
Jan 2017 年 4 月 17 日
Please post the complete command: How do you call fread?

サインインしてコメントする。

回答 (2 件)

Sanjana Ramakrishnan
Sanjana Ramakrishnan 2017 年 4 月 17 日
The answer posted in the below MATLAB Answers link would address your requirement:
https://www.mathworks.com/matlabcentral/answers/100727-how-can-i-read-a-binary-file-containing-records-with-multiple-formats-in-matlab-7-9-r2009b

Image Analyst
Image Analyst 2017 年 4 月 17 日
Just call fread() multiple times, once for each data type. Like call it once for an integer then once for a 2-element long array of doubles, then on to the next row and you call it once for an integer then again for a pair of doubles and so on until the whole array is read. For example, see this snippet:
% Go to the beginning of the file.
% Shouldn't be necessary, but can't hurt.
fseek(fileHandleID, 0, 'bof');
% Read the total number of voxels in the image.
% Read bytes 1-4.
stHeader.TotalVoxels = fread(fileHandleID, 1, '*int32');
% Note: this may be unreliable, and can be zero!
% Better to take the x, y, and z sizes and multiply them together.
% The next 4 bytes are the number of dimensions - 2 or 3.
% Read bytes 5-8.
stHeader.NumberOfDimensions = fread(fileHandleID, 1, 'int32');
% Read in the dimensions for the different directions.
% They'll be in bytes 9-20.
stHeader.x_size = fread(fileHandleID, 1, '*int32');
stHeader.y_size = fread(fileHandleID, 1, '*int32');
stHeader.z_size = fread(fileHandleID, 1, '*int32');
stHeader.TotalVoxels = stHeader.x_size * stHeader.y_size * stHeader.z_size;
% Read in the position of the center of the first pixel.
% They'll be in bytes 21-32.
stHeader.XStart = fread(fileHandleID, 1, '*float');
stHeader.YStart = fread(fileHandleID, 1, '*float');
stHeader.ZStart = fread(fileHandleID, 1, '*float');

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by