How to read binary array from text file

5 visualizaciones (últimos 30 días)
charles moore
charles moore el 4 de Jul. de 2017
Respondida: Kris Schweikert el 30 de Nov. de 2022
I have a special file format for data collection that I'm trying to automate importing from since I have quite a few of these files. The files are a mixed format of a text header followed by binary data. The header reads as follows "ASL 3.4 .rwb Binary File consists of 500 byte header followed by a 2D array of numbers. The numbers are single precision 32 bit floating point in Motorola (network) byte order. The column labels are as follows:. ..." there are 13 columns.
I can't seem to read the information that follows the header. I've tried to get the first 10 lines of data by inputting
>>data=fread(fileid,[10,13],'single',500,'ieee-be');
But the text does not align with anything that would reasonably make sense. I've tried changing to offset to 501 and 499, and neither show any improvement either. What am I missing?
  3 comentarios
Massimiliano Curzi
Massimiliano Curzi el 16 de Abr. de 2020
Hi Charles, did you manage to parse this data? I'm running in the same problem.
Max
Walter Roberson
Walter Roberson el 16 de Abr. de 2020
a sample file would help. you can zip and attach the zip

Iniciar sesión para comentar.

Respuestas (1)

Kris Schweikert
Kris Schweikert el 30 de Nov. de 2022
Hi,
a little late, but it might be helpful for others:
As I assume you have binary data from an ASL-5000 simulator, u can use the following code to read the full file:
fid = fopen(filename);
s = dir(filename);
fileSize = s.bytes;
recordSize = 13; % there are 13 columns in your example
noOfREcords = (fileSize-500)/recordSize;
fseek(fid, 500, "bof"); % skip header of 500 bytes
data = fread(fid, [noOfREcords,recordSize], '*float','ieee-be');
If you want to read the data line by line use
fid = fopen(filename);
s = dir(filename);
fileSize = s.bytes;
recordSize = 13;
noOfREcords = (fileSize-500)/recordSize;
fseek(fid, 500, "bof"); % skip header of 500 bytes
while ~feof(fid)
currData = fread(fid, [noOfREcords,recordSize], '*float','ieee-be');
if ~isempty(currData)
% do something with the data
end
end
As pointet out by Jan, skip is NOT an offset, but defines the number of bytes skipped after reading each value. Use fseek to define an offset for skipping the header in the ASL-file instead.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by