How do I continue to read files in a loop when an error occurs?

4 ビュー (過去 30 日間)
Aru E
Aru E 2020 年 11 月 29 日
回答済み: Sreeram 2025 年 5 月 14 日
Hello, I am trying to read .FIT files provided by my teacher and when reading the last bunch of them an error occurs. I tried using a try catch to continue, and it skips over and continues the loop, but I am not getting my value D.
Error using fitsinfo>openFile (line 760)
Unable to open file. File might be corrupt or filename might have unsupported characters.
for l = 100:999
try
fit2file = sprintf('x.000000%d.FIT',l);
D = fitsread(fit2file);
catch
end
end
Thank you in advance
  2 件のコメント
Mario Malic
Mario Malic 2020 年 11 月 29 日
Try reading the file that causes the error to check if it's corrupted. If It's not, consider renaming your files, 'x.00001.FIT' could be prone to failures.
Walter Roberson
Walter Roberson 2020 年 11 月 30 日
your loop overwrites D with the last file content it was able to read, but does not define D if no files could be read.

サインインしてコメントする。

回答 (1 件)

Sreeram
Sreeram 2025 年 5 月 14 日
Hi Aru,
I would like to second both Walter’s and Mario’s comments. In addition to their suggestions, I noticed that the same error message is thrown when a FITS file is inaccessible in MATLAB:
D = fitsread("nonexistent.fits");
Error using fitsinfo>openFile (line 760)
Unable to open file. File might be corrupt or filename might have unsupported characters.

Error in fitsinfo (line 212)
fid = openFile(filename);

Error in fitsread (line 103)
info = fitsinfo(filename);
So, you might want to double-check whether the file actually exists and is accessible. If the files are not accessible, you can add the directory containing the FITS files to the MATLAB path using "addpath".
Thanks!
Sreeram

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by