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

조회 수: 12 (최근 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 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:

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 CenterFile Exchange에서 Large 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