Why does this code fail?
    3 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
I have this code that looks something like the below (I cant upload the actual code). I get an error that says that: Error using fprintf Invalid File Identifier in fprintf(fid,'fgfdgfdgfd'). The weird thing is that this does not happen immediately after I run the code. The code runs smoothly and after 20 minutes of running or so I get this error. Any reasons why you would think this happen? 
Edit: when I set ii, jj, kk at the ones that the code failed at and generated that error, it works just fine...I dont understand why at some point it threw that error. This is the third time I attempt running the code and it still does this.
x = 1:1:10
y = 0.1:0.01:0.2;
z = 1:0.25:3;
for ii = 1:length(x)
    for jj = 1:length(y)
        for kk = length(z) 
            fid = fopen('for1234.dat','w');
            fprintf(fid,'fgfdgfdgfd')
            fprintf(fid,'ASHGGEfc')
            fprintf(fid,'fdWDG')
            fclose(fid)
            %AND THEN I RUN SOME PROCESSES using the system command
        end
    end
end
0 Commenti
Risposte (1)
  VBBV
      
      
 il 25 Gen 2023
        
      Modificato: VBBV
      
      
 il 25 Gen 2023
  
      fid = fopen('for1234.dat','w+');
fclose(fid)
Since you are writing the data repeatedly to the same file, place the above first line before all for loops and second line after all for loops.
5 Commenti
  Voss
      
      
 il 26 Gen 2023
				
      Modificato: Voss
      
      
 il 26 Gen 2023
  
			"Should I prehaps close the file before the system command?"
Yes. If the .dat file you're writing is used by the program you're calling in the system command, and you want to create a new .dat file on each iteration of your loops (i.e., each time you call the external program), then you should open and close the .dat file like you were originally doing.
Vedere anche
Categorie
				Scopri di più su Programming Utilities in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!