How to read binary array from text file

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 Commenti

Jan
Jan il 5 Lug 2017
Modificato: Jan il 5 Lug 2017
I do not understand the question. How exactly does the file look like? Where does the data block start? Why to you modify the skip to 501 and 499? Note that 500 is not an offset from the start of teh file, but this number of bytes is skipped after reading each value.
Hi Charles, did you manage to parse this data? I'm running in the same problem.
Max
a sample file would help. you can zip and attach the zip

Accedi per commentare.

Risposte (1)

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.

Richiesto:

il 4 Lug 2017

Risposto:

il 30 Nov 2022

Community Treasure Hunt

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

Start Hunting!

Translated by