MATLAB: Too many files open error yet I am closing files

MATLAB

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

Best Answer

"...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