invalid file identifier when more text files are introduced

10 views (last 30 days)
hi, I'm new to matlab. I wrote a code that: 1) reads many text files 2)writes new text files using data from the files it read; 3) plots the data from the newly written files.
The code works, but only with a limited number of text files (~60). When I have it read a large number of text files, this is the error:
--------------- Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Copy_of_readall_becca (line 138)
java.io.FileNotFoundException: /Users/Becca/.matlab/R2011b/matlab.prf (Too many open files) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.<init>(FileOutputStream.java:179) at java.io.FileOutputStream.<init>(FileOutputStream.java:131) at com.mathworks.services.Prefs.save(Prefs.java:305) at com.mathworks.services.Prefs$SavePrefsThread.run(Prefs.java:712) The desktop configuration was not saved successfully >>
Any help would be appreciated.

Answers (5)

Matt Kindig
Matt Kindig on 22 Mar 2012
I'm not that familiar with the Java errors that Matlab can throw, but this one looks like you have too many open file identifiers. After you are done processing each file (in the loop), you should close the file, like this:
fclose(fid)
where fid is your file identifier.

Jason Ross
Jason Ross on 22 Mar 2012
First, check that you at closing any things you are opening, as Matthew suggests. If you are doing that, check the outputs of the "limit" (or "limits") command on your machine. The "descriptors" number is the number of file handles you are allowed to have open. You may need to raise this number.
You can check how many are in use using a technique similar to the following:

Becca
Becca on 22 Mar 2012
Thanks , that did help , but it still says there are too many files open. I think the problem might be because there are nested for loops? Example:
For i=1:nfiles,
fid=fopen(file_to_be_read); fid1 = fopen(file_to_be_written) ;
Nested for loops here that use fid and fid1 ;
fclose(fid); fclose(fid1);
end

Becca
Becca on 22 Mar 2012
nevermind, got it sorted !! thanks guys! (first time posting a question on this site) .. I missed a fclose on one of the fid's.
cheers, becca

Walter Roberson
Walter Roberson on 22 Mar 2012
Becca's missing fclose is the most common cause of this kind of problem. It has been known, though, for MATLAB internal routines to "leak" file descriptors, to run out of slots to open files even though fopen('all') shows no files open. If you have an fopen / fclose loop that runs out after fid = 15, or after fid = 255, then you are probably encountering a combination of MATLAB bug and operating system limit.

Categories

Find more on Text Data Preparation 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!