0

So I'm trying to work with some data files that contain a text header followed by binary data, 16 bits signed integers with the least significant byte first.

I can't seem to open the binary data correctly. The text header is of variable length. I've tried the following, but my problem is that the data is not actually stored as binary, but is already a number, but not correctly. The header is of variable length so I can't tell it to read after so many characters without first opening the file.

fileName = 'PATH/TO/FILE/FILE_NAME.DAT';
dataFile = fopen(fileName);
header = '';
i = 1;
%dataContents = fileread(fileName);
dataContents = fread(dataFile);
while i < 115
    char = dataContents(i);
    header = [header char];
    if char == '}'
        break
    end
    i = i + 1;
end
header = header(2:end-1);
headerSplit = strsplit(header,',');
fileSize = str2double(headerSplit(17)); 
binaryData = dataContents(i:end);
data = [];
j = 1;
num = binaryData(1:50)
while j < fileSize
    data = [data, bin2dec(num2str(binaryData(j:j+1)))];
    j = j + 2;
end
length(data)

Any help would be great. I'm new to matlab so I'm probably missing something simple.

1 Answer 1

1

Without knowing the data format, it's nearly impossible to give a detailed advice. You probably need to set the precision argument of fread according to your data format.

After parsing your header, you know where your data begins. Use fseek(dataFile, numel(header)+2, 'bof') to set your file handle to the position where the file begins. Then start reading again using fread with a matching precision.

Sign up to request clarification or add additional context in comments.

1 Comment

by precision you mean decimal precision?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.