Date: Aug 1, 2013 2:35 AM
Author: Mike
Subject: Re: parfor: keep outputs with same output filename

Hi

I encounter this problem again and review your suggestion. Allow me to reiterate the problem.
What I want to do is to run a fortran .exe file by using matlab-created input parameter file. And I'd like to run it thousand times and be fast.
Since that fortran .exe read and write both fixed filenames:'inputpar.dat' and 'fort.30' (this fortran program causes me headache so I don't want to compile/link it again), I can't create various input filenames and also expect have various output filenames. Hence, I decide to do it by creating various directory, and creating input file and output file in that directory. At last, collect all output files (fort.30). Although it works, it tends to be lengthy. I am wondering if this can be improve or better thought?

Here is:
% run FORTRAN program 'Console2.exe' with input parameter file 'inputpar.dat'
% created by matlab. And run many times parallel. Console2.exe will always read
% 'inputpar.dat' and write 'fort.30'
nimg=100;
outArr=zeros(nimg,1);
matlabpool open local 3
t = cputime;
parfor i=1:nimg
% make several dir
dirname=['a' num2str(i)];
mkdir(dirname);
% cd every dir and create input parameter file
oldfolder=cd(dirname);
% input parameter file to be run by 'Console2.exe'
[fid1,msg]=fopen('inputpar.dat','w'); % be read by Console2.exe
fprintf(fid1,'%2d\n',i);
fclose(fid1);
% cd dir and run fortran .exe file and save in a matrix
% run FORTRAN program 'Console2.exe' with the above file
dos('D:\temp\Console2\Console2\Debug\Console2.exe<inputpar.dat');
% import output file 'fort.30' of 'Console2.exe'
out=importdata('fort.30');
outArr(i)=out;
cd(oldfolder);
% remove dir
dos(['rmdir/s/q ' dirname]);
end
matlabpool close
e = cputime-t;
fprintf('cputime: %f \n',e);
save('z.mat','outArr');