MATLAB: In parfor, how to create a temp folder for every worker

parallel computingparforsimulationsimulink

Hallo all,
Summarized is: I use parfor to running a simulink model, after running of the model come out some temp files. But because multiple workers attempt to write to the same file, that can sometimes an error results. So i remove these files every time after running of this model. But by next time this model is called, some files will compiled and run again (e.g. some C files), so that the running time of this model is very long. (I don't know well about simulink, so maybe i described not exactly)
My idea is to create a temp folder for each worker, so that there is no conflict between workers. And all files for the model will only once installed at the beginning it's called. So will time saved.
(update1) i wrote code like this
tmpdir = tempname
mkdir(tmpdir)
cd(tmpdir)
sim('XRSimWWPen01');
so there is a different folder for each worker and each loop. What i want to do is, that i create only four folder(four core) for four workers.

Best Answer

Hi,
I would use the process ID for that:
matlabpool open 4
parfor i=1:20
w=getCurrentWorker;
mydir = fullfile(tempdir,['myfolder',num2str(w.ProcessId)]);
disp(mydir)
end
(Note: tempdir is a MATLAB function not the variable from your example)