MATLAB: How to use SAVE with a PARFOR loop using Parallel Computing Toolbox

1-d8103hParallel Computing Toolbox

I would like to save some variables to MAT files from inside a PARFOR loop. However I get an error:
??? Error using ==> parallel_function at 598
Error in ==> parallel_function>make_general_channel/channel_general at 894
Transparency violation error.
See Parallel Computing Toolbox documentation about Transparency.

Best Answer

Transparency is violated by the SAVE command because in general MATLAB cannot determine which variables from the workspace will be saved to a file.
The solution is to move the call to SAVE to a separate function and to call that function from inside the PARFOR loop. Pass any variables that are to be saved as arguments to the function. That way MATLAB can determine which ones will be saved and transparency is not violated.
For example:
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
Similarly, for loading several files with enumerated file names, we can use the following code.
function [output] = par_load(i)
eval(['load file_' num2str(i) '.mat']);
output = time;
end
Which can be run with:
parfor i=1:4
data = par_load(i);
a(i) = 10 + data(1);
end