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

Jan
Jan 2017년 7월 5일
편집: Jan 2017년 7월 5일
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
Walter Roberson
Walter Roberson 2020년 4월 16일
a sample file would help. you can zip and attach the zip

댓글을 달려면 로그인하십시오.

답변 (1개)

Kris Schweikert
Kris Schweikert 2022년 11월 30일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

질문:

2017년 7월 4일

답변:

2022년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by