Search code examples
matlabfile-iobinaryfilesmat-file

Loading huge binary file partially into Matlab


I have a huge binary file with double precision numbers and I would like to load parts of it into Matlab. Is there a way to do this?

One way would be if I could convert it to a .mat file (without loading it in Matlab first), but I haven't been able to figure out how (or if it's actually possible).

Any ideas?

PS: I was thinking of using c++ to do the conversion but it turns out this is really problematic because I'm using a linux version of c++ (through cygwin) and a windows version of Matlab.


Solution

  • If you know what parts of the file you want to load, you can use fseek followed by fread (both preceded by fopen, of course).

    For example, jump a few thousand bytes into a file and read a certain number of bytes, as doubles:

    fid = fopen('binary.dat','r');
    fseek(fid, 3000, 'bof');
    A = fread(fid, N, 'double');
    fclose(A); % don't forget to close the file
    

    See the section of documentation called Reading Portions of a File for more information.