MATLAB: Does FOPEN fail when trying to open very large files from MATLAB

bigenormousfilefopenhugeintlargelongMATLABpointer

Why does FOPEN fail when trying to open very large files from MATLAB?
How can I read/write a file whose size is with greater than 2,147,483,647 bytes (=2^31-1) in MATLAB using FOPEN and FREAD/FWRITE? FOPEN always returns -1, which indicates an invalid file id (FID).

Best Answer

This is a limitation in the FOPEN routines in MATLAB, due to the underlying Standard C Library implementation of FOPEN. This implementation only guarantees the ability to address memory in files of less than 2^31 - 1 bytes. The reason behind this limitation is that a 32-bit integer is used to represent the position in the file. For more details, see page 230 of "The Standard C Library" by P.J. Plauger (<http://www.amazon.com/exec/obidos/tg/detail/-/0131315099/103-3235037-9951021?v=glance>).
You can workaround this C limitation by breaking up your file into smaller sections, preferably on boundaries which are logical divisions of your data. It may be also be possible to use non-Standard library functions to address larger files. You would need to create a C MEX-file to use this second option.