Too many files open error yet I am closing files

11 views (last 30 days)
I am reading in data from 100's of files, processing it and creating ~10 plots from the results of the data processing.
I have a function I created to read a data file and you will see that after I complete reading each file I close the file using fclose(fID).
function [digitizedData, nSamples] = readData(fNameSTR,varargin)
if nargin == 2
norm = varargin{1};
else
norm = false;
end
nFiles = length(fNameSTR);
nSamples = zeros(1,nFiles);
for file = 1:nFiles
fID = fopen(fNameSTR{file});
nSamples(file) = fread(fID,1,'int32','b');
if file == 1
digitizedData = zeros(nSamples(file),nFiles);
end
digitizedData(:,file) = fread(fID,nSamples(file),'double','b');
if norm
maxData = max(digitizedData(:,file));
digitizedData(:,file) = digitizedData(:,file)/maxData;
end
end
fclose(fID);
end
Once I plot results I save the plots using saveas() as shown in this function.
function figInfo = savePlot(fig,figInfo)
if isempty(figInfo.dirnameSTR)
fnameSTR = sprintf('/Users/Chip/Desktop/Figures/Figure_%d.png',figInfo.num);
else
if strcmp(figInfo.dirnameSTR(end:end),'/')
fnameSTR = sprintf('%sFigure_%d.png',figInfo.dirnameSTR,figInfo.num);
else
fnameSTR = sprintf('%s/Figure_%d.png',figInfo.dirnameSTR,figInfo.num);
end
end
saveas(fig,fnameSTR);
figInfo.num = figInfo.num+1;
end
I then do a complete clear & close all before starting over again with another set of data. So that should also clean up anything left open.
I am finding now that after running for an hour or so that Matlab tells me that I have too many files open and nothing I do other than quitting and restarting fixes this problem.
Perhaps fclose() is not working properly or the saveas() is not closing the file. Or perhaps there is something in the background that is causing this issue.
The error message indicates that the file limit is set by the OS however, I am not certain how to determine what files Matlab feels are open nor do I have a clue how to determine the limit on number of open files (I am running Matlab on Mac OS) much less how to change this limit.
Any ideas?? I am running MATLAB 2017b
  1 Comment
Stephen23
Stephen23 on 19 Mar 2018
"Perhaps fclose() is not working properly or the saveas() is not closing the file"
I doubt that very much. Have you looked at your code?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 19 Mar 2018
Edited: Stephen23 on 19 Mar 2018
"...you will see that after I complete reading each file I close the file using fclose(fID)."
Nope. You actually only close the very last file, the one that was opened on the last loop iteration:
for file = ...
fID = fopen(...);
...
end
fclose(fID)
If you want to close each file that you opened then you will need to move the fclose inside the loop:
for file = ...
fID = fopen(...);
...
fclose(fID)
end
  3 Comments
Jan
Jan on 19 Mar 2018
Edited: Jan on 19 Mar 2018
@George: This is a frequently occurring bug. I copy with it by typing this at once in every case:
fopen(FileName, 'r')
fclose(fid);
Afterwards I fill in the code between the lines following the rule, that neither error() nor return will leave this block. Never open a file in one (sub)function and close in in another one, but fopen and fclose must occur in the same block and the same indentation level.
Another rule: Never call fopen without checking the success. I've seen too many codes failing due to accessing a bad file identifier.
Steven Lord
Steven Lord on 19 Mar 2018
You could close the file using an onCleanup object.
fid = fopen(FileName, 'r');
closeTheFile = onCleanup(@() fclose(fid));
% Do stuff
delete(closeTheFile)
You don't have to explicitly call delete if you want the file to be closed only when the function call terminates. But if you want more fine-grained control over when the object is deleted you can call delete explicitly and force the cleanup function to be executed.
If you exit the function using return or due to an error the onCleanup object will be destroyed and its cleanup function will be executed.

Sign in to comment.

More Answers (0)

Categories

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

Tags

Products

Community Treasure Hunt

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

Start Hunting!