Clear Filters
Clear Filters

Too many open files. Close files to prevent MATLAB instability.

46 views (last 30 days)
Below is part of a code that I ran. It show this error while ii reaches 1949.
Too many open files. Close files to prevent MATLAB instability.
Caused by:
Message Catalog MATLAB:FileIO was not loaded from the file. Please check file location, format or contents
>> ii
ii =
1949
Should I use fclose() to close that open file after a loop? Will that affect the run?
More importantly, is there any way I can continue the run from 1949 instead of from 0? Thank you.
Thank you very much.
%% loop files to extract saturation
for ii = 1:1:8767
grabfile = filenames(ii,:);
[fid,message] = fopen(grabfile,'r','ieee-be'); % (filename,permission,format)
runnum= str2num(grabfile(end-8:end-4));
% Read domain spatial information
x1 = fread(fid,1,'double'); %Lower X
y1= fread(fid,1,'double'); %Lower Y
z1 = fread(fid,1,'double'); %Lower Z
nx = fread(fid,1,'int32'); % NX
ny = fread(fid,1,'int32'); % NY
nz = fread(fid,1,'int32'); % NZ
dx = fread(fid,1,'double');
dy = fread(fid,1,'double');
dz = fread(fid,1,'double');
ns = fread(fid,1,'int32'); % num_subgrids
% Loop over number of subgrids
for is = 1:ns; %number of subgrids
% Read subgrid spatial information
ix = fread(fid,1,'int32');
iy = fread(fid,1,'int32');
iz = fread(fid,1,'int32');
nnx = fread(fid,1,'int32'); % nx
nny = fread(fid,1,'int32'); % ny
nnz = fread(fid,1,'int32'); % nz
rx = fread(fid,1,'int32');
ry = fread(fid,1,'int32');
rz = fread(fid,1,'int32');
% Read Pressure data from each subgrid
for k=(iz+1):(iz+nnz);
for j=(iy+1):(iy+nny);
for i=(ix+1):(ix+nnx);
satur(i,j,k) = fread(fid,1,'double');
end % i
end %j
end %k
end %is
%now grabbing each z layer multiply each cell by porosity and sum
%volumetric moisture content for that layer
for k =1:NLAYER
sq_satur= squeeze(satur(:,:,k));
sq_porosity= squeeze(porosity(:,:,k));
vol= sq_satur .* sq_porosity;
tot_vol(1,ii) = runnum;
tot_vol(k+1,ii)= sum(sum(vol));
cumulative_vol= sum(tot_vol(1:ii));
end
end
  4 Comments
Walter Roberson
Walter Roberson on 13 Nov 2021
... eventually. But it would not be unusual to have a routine responsible for opening a file and returning some kind of identifier (number or object) for use by other functions. One could imagine, for example, an "open next file" function
Stephen23
Stephen23 on 13 Nov 2021
"so in this case, I will have to put a "fclose(fid)" at the every end of the loop?"
Not just in this case, in every case where you FOPEN a file you need to also FCLOSE the file.

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Nov 2021
Edited: Jan on 12 Nov 2021
Yes: call fclose(fid) after the data are imported.
The operating system limits the number of files, which can be open simultaneously. Close the files as soon as possible.
I'm using the technique to close a file in the same function and same level of indenation in general. If an error occurs during the processing the file is closed either during the error handling with try catch or an onCleanup cares for the closing.
By the way, would this work:
% Replace:
for k=(iz+1):(iz+nnz);
for j=(iy+1):(iy+nny);
for i=(ix+1):(ix+nnx);
satur(i,j,k) = fread(fid,1,'double');
end % i
end %j
end %k
% by:
satur((ix+1):(ix+nnx), (iy+1):(iy+nny), (iz+1):(iz+nnz)) = ...
fread(fid, nnz * nny * nnx, 'double');
  2 Comments
Anh Mai
Anh Mai on 13 Nov 2021
so in this case, I will have to put a "fclose(fid)" at the every end of the loop (right before the very last "end")?
Walter Roberson
Walter Roberson on 13 Nov 2021
Yes. If you open a file in a loop, you should close it in the loop. (With the narrow exception that you might be searching for a particular file with the intent to leave the desired file open upon exit from the search loop: in such a case you should be sure to close any file that you no longer need.
It is uncommon to open a file in a function and leave it open... at least not without returning a file identifier. Because of that, in many situations if you open a file and there is any chance of encountering an error then it can be a good idea to set up an onCleanup() to close the file.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!