Why will Matlab read only 2000 of my 5000 files?
16 ビュー (過去 30 日間)
古いコメントを表示
Hi. I have 5000 gridded ascii files that are output from a Fortran program. One is attached as an example ("Run2000.txt"). They are all in the same ESRI-ASCII format, and I have written the attached function "readESRI_ASCII_DepositFile.m" to separate the header values from the gridded data. That function is called repeatedly by the attached script, "read_arrivaltimes.m" to read each of the 5000 files and create a .mat file with the combined output. Some of the files are bad, so there is a "try . . . catch" loop. If a file fails to load, the script writes out "couldn't open xxxx".
The script runs fine for a certain number of (n) files. After that, it says "couldn't open xxxxx" for all the rest. Sometimes n=1982, other times n=1750, or some other number. Most of the remaining files are in the proper format and are readable if I start the loop at, for example, file #2000.
I thought perhaps I was exceeding some memory limit, but my Window Performance Monitor shows only 73% of my computer memory is being used.
My last attempt ended with the error message "Unrecognized function or variable: 'internal.matlab.datatoolsservices.WSBUtil.getWorkspaceDisplay'. Attached is a screenshot. Oddly, my workspace values do no display.
Do you have any idea what the problem could be? Thanks.
1 件のコメント
dpb
2025 年 9 月 5 日
Attach the m file scripat as text and format so can read.
A guess of the cause is you're not releasing the file handles of the read-in files and are running out of system resources.
採用された回答
Stephen23
2025 年 9 月 5 日
編集済み: Stephen23
2025 年 9 月 5 日
Yeah, dpb is likely on the right track.
"Why will Matlab read only 2000 of my 5000 files?"
What is likely happening is that your code opens a file in readESRI_ASCII_DepositFile, starts parsing it and then somewhere within readESRI_ASCII_DepositFile an error occurs (which is very likely, given the fragile way that code is written), and this error is then caught (and hidden) by the TRY within read_arrivaltimes, which means that you will accumulate more and more open files with each error (because you do not fclose those ones) until your OS cannot handle all of those open files any more (this depends on the OS, not MATLAB), after which all new fopen requests will fail. There is nothing in your code design to prevent this.
Using TRY to hide all errors like that is https://en.wikipedia.org/wiki/Anti-pattern and has a strong https://en.wikipedia.org/wiki/Code_smell . You are finding out why.
Best solution: get rid of those try-catches around everything.
Or if you want to continue writing shotgun code, use fclose('all')
5 件のコメント
dpb
2025 年 9 月 6 日
In similar veitn,
function [headervals,output_grid,errstat]=readESRI_ASCII_DepositFile(infile)
%readESRI_ASCII_DepositFile: Function that reads an ESRI_ASCII
%deposit file and returns the contents of that file
% infile=name of file to be read
% headervals(6) = values of parameters in header:
% headervals(1) = NCOLS
% headervals(2) = NROWS
% headervals(3) = XLLCORNER
% headervals(4) = YLLCORNER
% headervals(5) = CELLSIZE
% headervals(6) = NODATA_VALUE
% output_grid = gridded data
% errstat = error status: 0=success, -1=failure
fid=fopen(infile);
if fid==-1 % documented fopen() failure return
errstat=fid;
fprintf("failed opening input file %s\n",infile);
return
end
%If I can open it
headervals = NaN(6,1);
input_line = fgetl(fid);
headervals(1) = str2double(input_line(6:end));
input_line = fgetl(fid);
headervals(2) = str2double(input_line(6:end));
input_line = fgetl(fid);
headervals(3) = str2double(input_line(10:end));
input_line = fgetl(fid);
headervals(4) = str2double(input_line(10:end));
input_line = fgetl(fid);
headervals(5) = str2double(input_line(9:25));
input_line = fgetl(fid);
headervals(6) = str2double(input_line(13:end));
NCOLS = headervals(1);
NROWS = headervals(2);
NODATA_VALUE = headervals(6);
output_grid = NaN(NROWS,NCOLS);
for i=1:NROWS
output_grid(i,:) = fscanf(fid,'%f',[1 NCOLS]);
end
fclose(fid);
%Convert all elements that meet the NODATA_VALUE to NaNs
output_grid(output_grid<=NODATA_VALUE)=NaN;
return
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!