How to skip header consit of integer words

6 views (last 30 days)
The file I am trying to read has following description: The files were written in IEEE binary (big-endian). Each file contains three records described as follows:
rec 1: date and version number (8 4-byte integer words)
rec 2: gridded sst values in degC (360*180 4-byte real words)
rec 3: gridded ice concentration (360*180 1-byte integer words)
How can I read rec 2 and rec 3 data without worrying about the rec 1? I've tried using fseek but it's not working. For rec 2 I am using something like: sst=fread(fid2,[360,200],'real*4',1,'ieee-be'); but I am getting all crazy nos which doesn't make any sense.
Thanks.

Accepted Answer

dpb
dpb on 9 Jul 2014
fid=fopen('file','r','ieee-be');
fseek(fid,8*4);
temp=fread(fid,[360,180],'real*4','ieee-be').'; % no skip and fix size and orientation
ice=fread(fid,[360,180],'real*4','ieee-be').';
fid=fclose(fid);
Certainly after the first value skipping a byte will really screw everything after up until get thru the cycle of four bytes at which you should get one correct value again. Also remember Matlab fills the array in column-major order.
If after the above, data don't look like real floating point values, I'd suspect native format as the option.
  3 Comments
dpb
dpb on 9 Jul 2014
Edited: dpb on 9 Jul 2014
OOOPS! My bad; I cut 'n pasted and forgot to change the size. It says "1-byte integer" so I'd probably choose 'int8' or 'integer*1' but one presumes a concentration won't be negative so IA's 'uint8' will have the same effect.
Sorry for the oversight...
ADDENDUM
Also, just for the record note that memmapfile is useful for decoding such things, particularly if there are multiple sets of data within the file (C structs, iow).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!