Search code examples
arraysmatlabmemorymatrixhibernate-mapping

Matlab MemMapFile for a three dimensional matrix


I have a three dimensional matrix called "Shanto" of size (232,232,3052).

I want to memory map this, and am using the following command:

fileID = fopen('Shanto.dat','w');

fwrite(fileID, Shanto, 'single');

fclose(fileID) 

m = memmapfile('Shanto.dat')

However, when I try to access m.Data, I am given a 657083392 x 1 uint8 array.

How do I make it such that I can retain the (232,232,3052) shape of the original matrix?

Thanks,

Ben


Solution

  • When loading your .dat file you can specify the shape/format (the default is uint8 actually). You also need to specify the correct data format using fwrite:

    fileID = fopen('Shanto.dat','w');
    
    fwrite(fileID, Shanto, 'uint8'); %// Instead of 'single' as before.
    
    fclose(fileID) 
    
    m = memmapfile('Shanto.dat','Format',{'uint8',[232 232 3052],'MyFancyName'})
    

    You can then access the corresponding 3D array using m.Data.MyFancyName

    More info here