fprintf error after many iterations using GA algorithm
Show older comments
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!
1 Comment
Liset Geijselaers
on 17 Nov 2016
Accepted Answer
More Answers (0)
Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!