0

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

Daniel
  • 36,610
  • 3
  • 36
  • 69
merch
  • 945
  • 8
  • 19
  • [Don't use `eval`. Ever.](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170) – Adriaan Feb 17 '16 at 11:03
  • I'd love to avoid it, any idea for doing it? – merch Feb 17 '16 at 11:13
  • To be less strong on the previous statement: it's almost always a bad idea to use `eval`. The linked post shows you why: it's bad efficiency wise, bad for code readability, and possibly dangerous. In this case you can avoid `eval` completely, see Daniel's answer. – Adriaan Feb 17 '16 at 17:38

1 Answers1

3

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.

Daniel
  • 36,610
  • 3
  • 36
  • 69
  • Thank you very much, I think that is the answer I was looking for. I just need some clarifications. With matfile I will write within an existing (??) mat file a new/existing (??) cell array without having it in my workspace (or having just a pointer to the matfile, without a copy of the variable?). Is it correct? – merch Feb 17 '16 at 11:22
  • If `example.mat` already exists it will be extended, otherwise it will be created. Matfile is designed to work with files wich don't fit the memory. – Daniel Feb 17 '16 at 11:27