MATLAB Answers

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

188 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 (1)

Dolly more
Dolly more on 27 Dec 2018
Edited: Stephen Cobeldick on 4 Feb 2019
EVEN I AM GETTING THE SAME ERROR IN MY SIMPLE PROGRAN. DESPITE OF CLOSING ALL FILES. MY OPERATING SYSTEM IS WINDOWS XP. CAN ANYONE HELP ME.
My code::
close all
clear all
clc
f = fopen('narldatanames.txt','r'); %% reading binary files from the emsembled file
fo1=fopen('rawdata.txt','w'); %output file
fo2=fopen('spectrum.txt','w');
fo3=fopen('others','w');
for i=1:1677 %% to read one file at a time
m=fscanf(f,'%s',1);
fid=fopen(m,'r','l');
a=fread(fid,31,'int16'); %% to read headeer
dtype=a(31);
if dtype==1 %% 1 is raw data
fprintf(fo1,'%s\t\n',m);
elseif dtype==2 %% 2 is spectrum data
fprintf(fo2,'%s\t\n',m);
else
fprintf(fo3,'%s\t\n',m) ;
frewind(fid) %% to start from first line of the file
end
end
fclose(fid)
fclose(f)
fclose(fo1)
fclose(fo2)
fclose(fo3)

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!