Error in using try catch

1 回表示 (過去 30 日間)
farzad
farzad 2019 年 6 月 20 日
編集済み: Adam Danz 2019 年 6 月 20 日
Hi All
I have a loop to be forced to continue , and I get an error after the first cycle , since in the beginning of each try I put
clear all and, close all
cause I need to remove the data from the previous run, but this also removes the counter j in the loop. if I don't do that the previous data will interfer with the next
how do I do that ?

採用された回答

Adam Danz
Adam Danz 2019 年 6 月 20 日
編集済み: Adam Danz 2019 年 6 月 20 日
In general you should avoid using clear all (see link and image below). You're throwing the baby out with the bathwater.
If you need to remove data within a loop, you can either reset the variables with preallocation (see method 1), empty the variables (see method 2 below) or clear a list of variables in a controlled manner (see method 3).
% 1) reset the variables with preallocation
% Recommended method
for i = 1:n
myMat = nan(1,n); % a vector of NaNs
myCell = cell(1,n);
...
end
% 2) empty the variables.
for i = 1:n
myMat = [];
myCell = {};
myStruc = struct();
...
end
% 3) clear a hard coded list of variables
vars2clear = {'myMat','myCell','myStruc'};
for i = 1:n
clear(vars2clear{:})
...
end

その他の回答 (0 件)

カテゴリ

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