Memory issue with Matlab: update variable in .mat

岁酱吖の 提交于 2019-12-11 14:24:16

问题


I am working with a very computational expansive code in Matlab. It requires the usage of optimisation techniques and long computations using very big matrixes.

I am having the following issue: even if the code run correctly, at the end of the iterations required by the code, Matlab is not storing the biggest cell arrays that I have. I guess that it is due to some memory inefficiency in my code or with my computer (which is probably not sufficiently powerful). However, I followed all the general suggestions in the Matlab documentation and it is still not working.

Using evalc, I managed to save a different variable for each iteration of the code, in order to re-create the original matrix at the end of the loop. However, using:

  1. evalc(strcat('var_', mat2str(i), '=varTmp'));
  2. evalc(strcat('save(var_', mat2str(i), '-append)'));
  3. clear var*

.. in this way it is working, but it still slow and not very "clean and tidy".

Is there a way to do the same thing in a better way (consider that I have to do the same thing for several variables with different names and dimensions) or i.e. to update a cell array in a .mat file adding a column (row or whatever) without loading it?

Thanks


回答1:


Use matfile which allows you writing and reading parts of a mat file without reading it into the memory. A small demonstration:

%initialize matfile
data=matfile('example.mat','writable',true)
n=10
%preallocate cell
data.list=cell(n,1)
for ix=1:n
    %do some stuff
    var=foo(n)
    %store the results
    data.list(3,1)={var}
end

The line data.list(3,1)={var} looks a little odd because matfile has some limitations when indexing, but it's "meaning" is data.list{3}=var.



来源:https://stackoverflow.com/questions/35454800/memory-issue-with-matlab-update-variable-in-mat

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