How to skip an input in a loop when an error is raised and continue with the next input of the loop?

1 view (last 30 days)
Cynthia Dickerson on 12 Jun 2018
Commented: Cynthia Dickerson on 12 Jun 2018
I am trying to evaluate the bias of a particular function. To do this, I have a series of nested loops that change multiple input variables. Normally everything goes fine, but for a couple of combinations of inputs, the main function (qb.m) raises an error (below):
if (abs(2*z0) > abs(za))
error(' -- Decrease skew sensitivity. --');
end
My function just runs the qb.m function over and over again, records the outputs, and does a little calculation. When it hits an illegal combination, though, the main function ends execution and displays "Decrease skew sensitivity". Is there a way to make Matlab just move on to the next iteration of the loop instead of ending execution?
My code looks basically like this:
if true
for u=1:Max_training
for v=0:dimension;
for j=1:8
newspec = [compass_points(j,:)];
for i=1:z
B=50*i;
[BTRAIN,CNTER]=replica(TNSPEC,B);
bias=sds-expected_standard_deviation(j);
SDS_matrix(((j-1)*z+i),:)=[compass_points(j,:),B,sds,bias,sdskew];
end
SDS_matrix
end
% Run that last block 5 more times, changing only the name
for j=1:8
newspec = [compass_points(j,:)];
for i=1:z
B=50*i;
[BTRAIN,CNTER]=replica(TNSPEC,B);
bias=sds-expected_standard_deviation(j);
SDS_matrix(((j-1)*z+i),:)=[compass_points(j,:),B,sds,bias,sdskew];
end
SDS_matrix
end
%Average those six runs
%Do some calculations
%Store answers in a cell array
end end end end end

Matt Dickson on 12 Jun 2018
You should make your loop code inside a try/catch block and catch the error. It would look like
for i = 1:100 % whatever your loop is
try
catch
% Nothing should happen, just move on
continue;
end
end
2 CommentsShow 1 older commentHide 1 older comment
Cynthia Dickerson on 12 Jun 2018
Thanks! You're my hero!!! That calculation is the last one I needed for my dissertation!!!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by