I have a binary file that contains records with varying formats. I would like to be able to specify the format of the records in a single call to FREAD, and have it return the correctly formated values.
For example, I might have a record that consists of a 16-bit integer and a 32-bit float created as follows:
% Create binary file with 16-bit integers and floats
fid = fopen('mybytestream','w');intstowrite = [50 100 150];floatstowrite = [111.2984 222.4589 333.4985];for k = 1:length(intstowrite) fwrite(fid,intstowrite(k),'int16'); fwrite(fid,floatstowrite(k),'float');endfclose(fid);
I'd like to read the file as follows:
fid = fopen('mybytestream'); out = fread(fid,inf,'int16+float'); fclose(fid);
where 'int16+float' is a format string specifying that the first 2 bytes are int16 and the next 4 bytes are float.
Best Answer