How can I load large files (~150MB) in MATLAB?

心不动则不痛 提交于 2019-11-28 07:46:46

Starting from release R2011b (ver.7.13) there is a new object matlab.io.MatFile with MATFILE as a constructor. It allows to load and save parts of variables in MAT-files. See the documentation for more details. Here is a simple example to read part of a matrix:

matObj = matfile(filename);
a = matObj.a(100:500, 200:600);

If your original file is not a MAT file, but some text file, you can read it partially and use matfile to save those parts to the same variable in a MAT file for later access. Just remember to set Writable property to true in the constructor.

Assuming your text file is tab-delimited and contains only numbers, here is a sample script to read the data by blocks and save them to MAT file:

blocksize = 100;
startrow = 0;
filename = 'test.mat';
matObj = matfile(filename,'Writable',true);
while true
    try
        a = dlmread(filename,'\t',startrow,0); %# depends on your file format
        startrow = startrow + blocksize;
        matObj.a(startrow+(1:blocksize),:) = a;
    catch
        break
    end
end

I don't have the latest release now to test, but hope it should work.

If it is an image file, and you want to work with it, try the matlab block processing. By using it, you will load small parts of the file. Your function fun will be applied to each block individually.

 B = blockproc(src_filename,[M N],fun)

In case it is an xml file, try the XML DOM Node mode together with SAX - (Thanks to @Nzbuu for pointing that out), but that seems to be an undocumented functionality.

Also, if it is a textual file of any kind (Unlikely, due to the amount of data), try external tool to split.

You can also user MATLAB's Memory-Mapping of Data Files to read in a block of the file, process it, and proceed to the next block without having to load the entire file into memory at once.

For instance, see this example, which "maps a file of 100 double-precision floating-point numbers to memory."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!