Parallel_Function error 598 and fprintf
Show older comments
I am running the parallel version of Genetic Algorithm and am running into some parallel issues. When I try to run on Parallel I recieve the error:
Error using ==> parallel_function at 598 Error in ==> filename at 63 Invalid format.
The line it refers to has a simple fprintf line where the file it is writing to is a simple text file:
fid=fopen('textfile_lab.txt','w');
i=1;
while i<=length(text)
Line 63 fprintf(fid,text{i});
i=i+1;
end
I have different text files for the four different labs that should be running simultaneously. Does any one have any ideas why this is coming up or how to fix it? Thank you.
7 Comments
Thomas Ibbotson
on 21 Jan 2011
Hi Jason,
That error message occurs when the second argument to the fprintf function is not a string. What determines the value of the 'text' variable in your code?
Jason
on 21 Jan 2011
Thomas Ibbotson
on 21 Jan 2011
If any of the filename_lab.txt files are empty then fgetl(fid) will return -1, which will cause the error you are seeing.
Jason
on 21 Jan 2011
Jason
on 21 Jan 2011
Thomas Ibbotson
on 21 Jan 2011
Are you able to reduce your code to a minimal example that reproduces the problem you are seeing? Does the code work without using the parallel features?
Jason
on 24 Jan 2011
Answers (3)
Walter Roberson
on 21 Jan 2011
0 votes
This was also asked in the newsgroup. My answer there was:
You are writing to a fixed filename in each of the parallel threads. Sometimes when you try to open the file, another thread will already have the file open. In such a case the fopen will fail and return a negative number. You do not test for that possibility, and you use the negative number in fprintf. fprintf recognizes that the negative number cannot be a valid file identifier and tries to make sense of it as a format instead.
To this I would add that 'filename_lab.txt' is taken literally. Try
sprintf('filename_%d.txt,lab)
to get a lab-dependent name.
5 Comments
Jason
on 21 Jan 2011
Walter Roberson
on 21 Jan 2011
Cross reference for getting the lab-dependent file name: http://www.mathworks.com/matlabcentral/answers/184-parallel-computing-and-save-function
Walter Roberson
on 21 Jan 2011
Text files should always be opened with 'wt' instead of 'w', and should be read with 'rt' instead of 'r'.
Walter Roberson
on 22 Jan 2011
Can you say more about the hidden characters? Are you able to obtain their value, such as by reading the file as binary and double() the string to determine the character values?
Jason
on 24 Jan 2011
Walter Roberson
on 22 Jan 2011
Your present code
i=1;
while i<=length(text)
fprintf(fid,text{i});
i=i+1;
end
can be replaced entirely by
fprintf(fid, '%s\n', text{:});
(Assuming, that is, that text is not actually a 2D cell array of strings.)
Walter Roberson
on 24 Jan 2011
0 votes
An answer from the newsgroup is that labindex is a constant for PARFOR and is only distinct for SMPD . A method to get the job id was shown in the newsgroup.
Categories
Find more on Environment and Settings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!