Friday, February 12, 2010

fread: read binary data

probably the moment that i felt like the biggest matlab stud (although there are so many i cannot even count, haha JOKE), is when i finally decoded the binary data from the laser scanner my flume. the folks at SAFL had written the program to store the voluminous data in binary format, and ever since dec. 2006 i knew that one day i would have to figure out how to read it.

fread reads data from a binary file.

numsamples = fread(fid, 1, 'long','ieee-le');

first, you probably need to use fseek to move around in the file (to skip header info, etc.):

status = fseek(fid, 3000, 'bof');

i love the words bof, cof, and eof (beginning, current position, and end of file)

anyway, then you can read all the data and write it out to a matlab matrix

for i = 1:numsamples
    mirrorrev(i) = fread(fid, 1, 'double','ieee-le');
    drumrev(i) = fread(fid, 1, 'double','ieee-le');
    distance(i) = fread(fid, 1, 'single','ieee-le');
    laserstatus(i) = fread(fid, 1, 'uint16','ieee-le');
    ambient(i) = fread(fid, 1, 'int8','ieee-le');
    amplitude(i) = fread(fid, 1, 'int8','ieee-le');
end

data I/O is very important!

No comments:

Post a Comment