Parallel_Function error 598 and fprintf
显示 更早的评论
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 个评论
Thomas Ibbotson
2011-1-21
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
2011-1-21
Thomas Ibbotson
2011-1-21
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
2011-1-21
Jason
2011-1-21
Thomas Ibbotson
2011-1-21
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
2011-1-24
回答(3 个)
Walter Roberson
2011-1-21
0 个投票
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 个评论
Jason
2011-1-21
Walter Roberson
2011-1-21
Cross reference for getting the lab-dependent file name: http://www.mathworks.com/matlabcentral/answers/184-parallel-computing-and-save-function
Walter Roberson
2011-1-21
Text files should always be opened with 'wt' instead of 'w', and should be read with 'rt' instead of 'r'.
Walter Roberson
2011-1-22
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
2011-1-24
Walter Roberson
2011-1-22
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
2011-1-24
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!