How to read binary array from text file

5 views (last 30 days)
charles moore
charles moore on 4 Jul 2017
Answered: Kris Schweikert on 30 Nov 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 Comments
Massimiliano Curzi
Massimiliano Curzi on 16 Apr 2020
Hi Charles, did you manage to parse this data? I'm running in the same problem.
Max
Walter Roberson
Walter Roberson on 16 Apr 2020
a sample file would help. you can zip and attach the zip

Sign in to comment.

Answers (1)

Kris Schweikert
Kris Schweikert on 30 Nov 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.

Community Treasure Hunt

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

Start Hunting!