MATLAB: Fmincon in parallel writing and reading same files/folder

fminconMATLABoptimizationparallel computing

Hi all,
I have an optimization problem using fmincon that tries to find a set of parameters that are written in a .csv file and then, there is a system call to run an external program that takes as input this .csv file and then generates the output files stored in a folder. These output files are read again in the same function after the system call and used to calculated the error objective function. Is there a way to do all this minimization problem in parallel? Because now I think, by doing parpool('local') and opts = optimoptions('fmincon','UseParallel',true) the different workers are trying to write at the same time in the same .csv file and then generating the output files in the same folder and this will lead to a bad behavior of the algorithm, isn't?

Best Answer

Have your objective function use tempname to generate the names of temporary files to use for the input and output files. For example,
infile = tempname();
csvwrite(infile, data);
outfile = tempname();
system( sprintf('ghoatbleet.exe < "%s" > "%s", infile, outfile) );
processed_data = csvread(outfile);
delete(infile);
delete(outfile);