A common task in image processing is reading and writing binary data to files. On the Matlab command line it comes in handy to have a set of helper functions that save one from typing duplicate code over and over again, i.e. the fopen(), fwrite()/fread() and fclose() stuff.

I have created two functions writebinary() and readbinary() for that purpose. A primitive session where we save and load the 2-D Shepp-Logan phantom could look like this:

  1. I = phantom(200) .* 256; % create the phantom
  2. writebinary(img.raw‘, I, ‘uint8); % save it as char
  3. J = readbinary(img.raw‘, [200 200], ‘uint8=>double); % read it in again and cast to double

Its not a big deal - but useful!

The latest version can be downloaded at MATLAB Central.