<email@example.com> wrote in message news:firstname.lastname@example.org... > Hi, > > Thank you all. > I've modified the section to > [fid1,msg]=fopen('inputpar.dat','wt'); % be read by Console2.exe > if fid1==-1 > error(msg) > end > fprintf(fid1,'%2d\n',i); > after parfor and re-run it, then also have the same message:
If you run this code BEFORE you enter the PARFOR loop, then when fid1 gets passed to the workers it's just a double scalar. They didn't open the file themselves, so they don't treat it as a file identifier. It's like you SAVEd the identifier to a MAT-file, restarted MATLAB, then LOADed the MAT-file.
If you run this code INSIDE your PARFOR loop, you're in a race condition situation. Don't do this.
As I said, I think your best options are to have each iteration write to a file with a unique, iteration dependent name and either store the values from that file into a variable inside the PARFOR loop or combine them into one file AFTER the PARFOR loop is done.