How to open binary file where each number has different precision?
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
답변 (2개)
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
댓글 수: 0
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');
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!