%% Transfer Files to parfor Workers
% When you run parfor with or without a parallel pool, by default, MATLAB performs an automatic dependency analysis on the loop body. MATLAB transfers required files to the workers before running the statements. In some cases, you must explicitly transfer those files to the workers. For more information, see Identify Program Dependencies.
% If you are using parfor without a parallel pool, use parforOptions to transfer files. Create a cluster object using the parcluster option. Create a set of parfor options with the cluster object using the parforOptions function. To transfer files to the workers, use the 'AttachedFiles' name-value pair.

cluster = parcluster;
opts = parforOptions(cluster,'AttachedFiles',{'myFile.dat'});

% Pass the parfor options as the second input argument to the parfor function. The workers can access the required files in the loop body.

parfor (i=1:2,opts)
    M = csvread('myFile.dat',0,2*(i-1),[0,2*(i-1),1,1+2*(i-1)]);
    out(i) = norm(rand(ceil(norm(M))*1e3));
end

% Copyright 2018 The MathWorks, Inc.