Too many files open error yet I am closing files
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Stephen23
2018년 3월 19일
"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?
채택된 답변
Stephen23
2018년 3월 19일
편집: Stephen23
2018년 3월 19일
"...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
Jan
2018년 3월 19일
편집: Jan
2018년 3월 19일
@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
2018년 3월 19일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!