MATLAB: Do I receive an ‘Out of Memory’ error using FREAD function when the file is smaller than the largest contiguous memory block

fopenfreadMATLABmemoryofoutuint16

I have a large binary file of uint16 data named fname. It smaller than the largest free memory block. When I execute the following commands:
fid=fopen('fname');
F=fread(fid,'uint16')
I receive the following result:
??? Error using ===> fread.
Out of memory. Type HELP MEMORY for your options

Best Answer

Here, the 'uint16' refers to how to interpret the data in the file and not how to represent it in MATLAB. Thus, class(F) will return double, so the FILESIZE * sizeof(double)/sizeof(uint16) will be larger than the memory block and out of memory error is expected.
To work around this issue, use the 'uint16=>uint16' option to FREAD to make MATLAB interpret the data as uint16 and also store it in MATLAB as uint16. This can be done using either of the following commands
1. F = fread(fid,'uint16=>uint16');
2. F = fread(fid,'*uint16');
Related Question