skip a load error

5 ビュー (過去 30 日間)
joseph Frank
joseph Frank 2011 年 3 月 12 日
コメント済み: Pippa Williams 2022 年 2 月 1 日
Hi,
I want to load some mat files in my folder according to a certain criteria. Sometimes this file doesn't exist. How can I continue to load the next file with causing the program to stop because of the error? example:
UID=unique(ID);
for i=1:size(UID,1) filename=['Data' num2str(UID(i,1))]; load (filename,'DataFinal')
%%% here I want to manipulate the data end
if load returns an error that such a file doesn't exist , how can I continue the loop without any interruption. Regards

採用された回答

Teja Muppirala
Teja Muppirala 2011 年 3 月 12 日
You can use a try (catch) block. For example
x = 5;
try
load('asdasdasd.mat') %I can't load this file
catch
disp('Did not load that last file, but it''s ok.');
end
x = x+1;
disp(x)
For more information:
help try
  1 件のコメント
Pippa Williams
Pippa Williams 2022 年 2 月 1 日
Awesome, thanks for the tip!

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

その他の回答 (1 件)

Matt Tearle
Matt Tearle 2011 年 3 月 12 日
You can use exist to check if the file exists, but perhaps another approach would be to use cellstr(ls('Data*')) to get a list of the files first, then loop over those.

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by