How can I read arbitrary files using only position offsets?

1 view (last 30 days)
I'm trying to read a file formatted without a file extension. It contains a few bytes of information at the start, then contains a very long (in the tens of millions) sequence of unsigned integers between 0 and 255. My end goal is importing this sequence in to matlab.
I've tried using fopen and the memory mapped file functions but they don't seem to be what I need.
  2 Comments
Walter Roberson
Walter Roberson on 27 Mar 2014
The unsigned integers, are they in text format or in single-byte binary ?
Jacob
Jacob on 28 Mar 2014
Binary, can't be gainfully opened in a text editor.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2014
fid = fopen('YourFile.bin', 'r'); %not 'rt' !!
fseek(fid, 1024, 'bof'); %move to offset 1024, where 0 is the first byte of the file
abc = fread(fid, 2, '*uint8'); %read two bytes
fseek(fid, 9875, 'bof'); %move somewhere else
def = fread(fid, 17, '*uint8');
and so on.

Community Treasure Hunt

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

Start Hunting!