Clear Filters
Clear Filters

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

4 views (last 30 days)
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 radfrac= Radfrac_min : Radfrac_step : Radfrac_max
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);
[sds,sdskew,qrr] =qb(TNSPEC,BTRAIN,newspec,CNTER,radfrac,sensitiv);
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);
[sds,sdskew,qrr] =qb(TNSPEC,BTRAIN,newspec,CNTER,radfrac,sensitiv);
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

Accepted Answer

Matt Dickson
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
% Your loop code here
catch
% Nothing should happen, just move on
continue;
end
end

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!