In matlab I would like to read from a structured, and rather big, file (size: 18+2048*2048 bytes), where 18 fist bytes are assigned to the header and the rest are pixel image intensities.
The concern here is the speed. As you see in the code below multiple access to the file has considerably slowed down the performance.
Can you suggest any faster way of reading these contents from the file? e.g. reading entire thing in a buffer from which we can read using the “fread” function.
fid = fopen(fileName, 'r', 'b'); % 'r' readonly and 'b' big endian
a= fread(fid,1,'uint16');
b1= fread(fid,1,'uint32');
b2= fread(fid,1,'uint32');
c1= fread(fid,1,'uint32');
c2= fread(fid,1,'uint32');
img=zeros (...
for i= (b1 + 1) : (b2 + 1)
for j= (c1 + 1) : (c2 + 1)
img(i, j) = fread(fid,1,'uint16');
end
end
Just calculate the total size n from b1, b2, c1, and c2, and pass it to a single
fread(fid,n,'uint16')call instead of looping over it. Then callreshapeon the output to make it a 2-d array.