Why do I receive 'Too many files open' error messages after several hours when executing code which involves reading text files?

67 views (last 30 days)
I am executing MATLAB code which involves reading several text files. I receive error messages after 10 hours. A snippet of code is provided below:
saveLo = fopen(ExportFile1,'r');
temp003=fscanf(saveLo,'%c');
% some calculations
saveLo = fopen(ExportFile2,'r');
temp003=fscanf(saveLo,'%c');
% and so on
The error messages received are provided below:
??? Error using ==> fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ==> JaganOpt1 at 386
??? C:\Program Files\MATLAB\R2007a\toolbox\matlab\graphics\hgfeval.m: Too
many files open; check that FILES = 20 in your CONFIG.SYS file.
Error in ==> scribe.legend.methods>bdowncb at 176
??? Error while evaluating axes ButtonDownFcn
??? C:\Program Files\MATLAB\R2007a\toolbox\matlab\timefun\etime.m: Too many
files open; check that FILES = 20 in your CONFIG.SYS file.
Error in ==> scribe.legend.methods>bmotion at 51
Error in ==> scribe.legend.methods>bmotioncb at 76
??? Error while evaluating figure WindowButtonMotionFcn
??? C:\Program Files\MATLAB\R2007a\toolbox\matlab\timefun\etime.m: Too many
files open; check that FILES = 20 in your CONFIG.SYS file.
Error in ==> scribe.legend.methods>bmotion at 51
Error in ==> scribe.legend.methods>bmotioncb at 76
??? Error while evaluating figure WindowButtonMotionFcn

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 1 Mar 2019
The reason for the error messages is because the number of files that are open exceeds the limit on the maximum number of files that can be kept open. There are several potential causes of this.
1) Make sure that, for every file you open with FOPEN, you close the file with a FCLOSE command when you are done using it.
Otherwise it is easy to quickly exceed the maximum number of open files. For example,
% too_many_open_files_script.m
for i = 1:1000
if fopen([num2str(i) '.mat'],'a') == -1
break
end
end
i
load('1.mat')
fclose('all');
>> too_many_open_files_script.m
i =
457
Too many open files. Close files to prevent MATLAB instability.
Caused by:
Message Catalog MATLAB:load was not loaded from the file. Please check file location, format or contents
Note that you may get a different number of maximum files.
2) While MATLAB does not have a limit on the maximum number of files that can be open, the operating system does. Consult your system administrator for information on how to change this limit.
3) Another possible scenario is that you have too many open connections on a network file server. If you are reading and writing files on a network file system, that file system or file server may have limits on file connections. Consult your system administrator for information on how to change this limit.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!