필터 지우기
필터 지우기

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

조회 수: 46 (최근 30일)
Anh Mai
Anh Mai 2021년 11월 12일
댓글: Stephen23 2021년 11월 13일
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
Walter Roberson
Walter Roberson 2021년 11월 13일
... 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 2021년 11월 13일
"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.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 11월 12일
편집: Jan 2021년 11월 12일
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
Anh Mai
Anh Mai 2021년 11월 13일
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 2021년 11월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by