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

6 visualizaciones (últimos 30 días)
Giulia Pazzi
Giulia Pazzi el 14 de Abr. de 2017
Respondida: Image Analyst el 17 de Abr. de 2017
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.

Respuestas (2)

Sanjana Ramakrishnan
Sanjana Ramakrishnan el 17 de Abr. de 2017
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 el 17 de Abr. de 2017
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');

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by