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!