I've found that
"hist.exp_time = sprintf('%2d-%2d-%2.0f',datime(4),datime(5),datime(6));"
doesn't make up the 10 bytes required, so I had to modify it to "%2.1f". But sometimes I still get a 347 byte hdr file. I'm not quite sure why that happens...
Thanks for these programs, I would never have been able to do this from scratch.