MATLAB: Fprintf error after many iterations using GA algorithm

fprintf errorga algorithmmany iterations

I have matlab script in which i have to write to several .dat files. For this i use fprintf. After a number of iterations, this is usually random, I get the error: 'Error using fprintf' 'Invalid file identifier. Use fopen to generate a valid file identifier'. I showed my code below:
fid5=fopen('track1.dat','w');
if fid5<3
clear fid5
fid6=fopen('track1.dat','w');
fprintf(fid6,'Sys\n');
fprintf(fid6,'==================================================================\n');
fprintf(fid6,'met\n\n');
fprintf(fid6,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n','x','y','h','V','T','m');
fprintf(fid6,'==================================================================\n');
fprintf(fid6,'%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.0f\n',INM_data');
fprintf(fid6,'\n==================================================================\n');
fprintf(fid6,'%6s\r\n',c.ET);
fprintf(fid6,'%6s\r\n',c.EM);
fclose(fid6);
clear fid6
else
fprintf(fid5,'Sys\n');
fprintf(fid5,'==================================================================\n');
fprintf(fid5,'met\n\n');
fprintf(fid5,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n','x','y','h','V','T','m');
fprintf(fid5,'==================================================================\n');
fprintf(fid5,'%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.0f\n',INM_data');
fprintf(fid5,'\n==================================================================\n');
fprintf(fid5,'%6s\r\n',c.ET);
fprintf(fid5,'%6s\r\n',c.EM);
fclose(fid5);
end
x_min_arr=round((min(x_arr)-8000)/100)*100;
x_max_arr=round((max(x_arr)+8000)/100)*100;
y_min_arr=round((min(y_arr)-8000)/100)*100;
y_max_arr=round((max(y_arr)+8000)/100)*100;
fid4=fopen('grid2D2.dat','w');
if fid4<3
clear fid4
fid7=fopen('grid2D2.dat','w');
fprintf(fid7,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\r\n','x_min','x_max','x_sz','y_min','y_max','y_sz');
fprintf(fid7,'==================================================================\n');
fprintf(fid7,'%10f\t%10f\t%10f\t%10f\t%10f\t%10f\r\n',x_min_arr,x_max_arr,500,y_min_arr,y_max_arr,500);
fclose(fid7);
clear fid7
else
fprintf(fid4,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\r\n','x_min','x_max','x_sz','y_min','y_max','y_sz');
fprintf(fid4,'==================================================================\n');
fprintf(fid4,'%10f\t%10f\t%10f\t%10f\t%10f\t%10f\r\n',x_min_arr,x_max_arr,500,y_min_arr,y_max_arr,500);
fclose(fid4);
end
status=system('INMmodel.exe track1.dat grid2D2.dat' );
delete('track1.dat','grid2D2.dat');
The error occurs at the line:
fprintf(fid7,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\r\n','x_min','x_max','x_sz','y_min','y_max','y_sz');
As can be seen I already added an check to see if matlab managed to open the file and if not I try it again with a different variable. I also remove the files after using them as for my variables in my workspace. Does anybody have an idea how I can solve this error because I am running out of options? Or is there an other way to write this type of data, including the text in table form, to a .dat file?
Thank you for your help!

Best Answer

You should be comparing against 0 for errors, not comparing against 3.
Your flow does not seem to make much sense. You try to open files, and if the open fails, you try to open again ?? Why do you do that? Are you trying to deal with "race conditions" where the file might be busy? If you are then there is no reason to expect that the condition will have cleared up by the time of the second fopen on the same file.
Related Question