Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save command in matlab parfor loop

I'm trying to save a matrix within a parfor loop. I know matlab doesn't allow this and one needs to create a separate program. A bit of googling suggest creating a separate function, say parfor as follows:

  function parsave(fname, x,y)

    save(fname, 'x', 'y')

   end

The problem I'm encountering is that the program seems to be halting without any error before going through all of the iterations (i.e., it is saving about half of the files). I've got quite a lot of small files (around 1M). Could this be the problem?

Thanks in advance

like image 280
Stuck_pls_help Avatar asked May 22 '26 11:05

Stuck_pls_help


1 Answers

Calling save directly within parfor violates transparency requirements. Instead you need to pass your variables to another function within which you can call save.

An example Matlab provides is the following.

Save the following as "parsave.m":

    function parsave(fname, x,y)
    save(fname, 'x', 'y')
    end

Then run it with:

    parfor ii = 1:4
    x = rand(10,10);
    y = ones(1,3);
    parsave(sprintf('output%d.mat', ii), x, y);
    end
like image 58
3 revs, 2 users 93%user1375 Avatar answered May 24 '26 01:05

3 revs, 2 users 93%user1375