fprintf error after many iterations using GA algorithm

17 views (last 30 days)
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
Liset Geijselaers on 17 Nov 2016
Hi everybody, I still have the same problem!
I changed the code a little, see below, and it worked good for a few optimization problems but now again I get an error when Matlab is trying to write to a file. The code is as follows:
[fid msg]=fopen('track.dat','w');
fprintf(fid,'Sys\n');
fprintf(fid,'==================================================================\n');
fprintf(fid,'met\n\n');
fprintf(fid,'%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n','x','y','h','V','T','m');
fprintf(fid,'==================================================================\n');
fprintf(fid,'%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.3f\t%10.0f\n',INM_data');
fprintf(fid,'\n==================================================================\n');
fprintf(fid,'%6s\r\n',c.ET);
fprintf(fid,'%6s\r\n',c.EM);
fclose(fid);
x_min=round((min(x_arr)-8000)/100)*100;
x_max=round((max(x_arr)+8000)/100)*100;
y_min=round((min(y_arr)-8000)/100)*100;
y_max=round((max(y_arr)+8000)/100)*100;
[fid2 msg]=fopen('grid2D.dat','w');
fprintf(fid2,'%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(fid2,'==================================================================\n');
fprintf(fid2,'%10f\t%10f\t%10f\t%10f\t%10f\t%10f\r\n',x_min,x_max,500,y_min,y_max,500);
fclose(fid2);
status=system('INMmodel.exe track.dat grid2D.dat' );
delete('track.dat','grid2D.dat');
As can be seen I open the files, I write something, close them and then even delete them.
I run the optimization problems for 200 generations and with a population of 50. The error occurs random, or at least I don't see any pattern in it. The message I get:
fid = -1
msg= Permission denied
And then with the next line I get an error in fprintf saying:
Invalid file identifier. Use fopen to generate a valid file identifier.
Does anybody have any idea why this is happening?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 27 Oct 2016
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.
  2 Comments
Liset Geijselaers
Liset Geijselaers on 27 Oct 2016
After the files are created and written and used the files are deleted again, so my thoughts are that the file is not busy anymore. This is why I try to open the same file twice. I have tried it with different filenames so if the first fopen does not work with the first file I try a new variable with a new filename, but this still gives the same results so I changed it back.
I have changed the error comparing to 0.
Walter Roberson
Walter Roberson on 27 Oct 2016
I suspect that somewhere in the code there is a leaking fopen(), one that is not fclose()'d . It might even be in the Mathworks code rather than in yours. If there is a file open which is not being closed then you would run out of available file descriptors, at either around 32 files (very old systems!) or 255 files (middle aged systems still in use sometimes) or 1024 files (active systems) or 4095 files.
I suggest putting in a try/catch and when it gets triggered, show
length(fopen('all'))
to find out how many open files you had.
Over the longer term, you should change each fopen() to use the form
filename = ...
[fid, status] = fopen(filename, 'w')
if fid < 0
error('Open of "%s" failed because "%s"\n', filename, status);
end

Sign in to comment.

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!