Skip instance of a loop with error

조회 수: 6 (최근 30일)
Gekkouga
Gekkouga 2020년 12월 29일
댓글: Gekkouga 2020년 12월 29일
Hi,
I am using nested loop to generate a range of variables and solve an equation.
for x = 1:1:20
for y = 1:1:20
for z = 1:1:20
fn = x+y+z; %The function is just a representation. I am actually performing a Finite Element Analaysis
ans = func(fn); %The equation is solved using an external function
end
end
end
The combination of variables (x,y,z) do not always form the root of the equation. When no solution is found, the Matlab function throws an error and exits the loop. However, I want the loop to continue and proceed checking the rest of the variable combinations. Is there a way to flag this error and continue through the looping process?
Thanks,
Abinav

채택된 답변

James Tursa
James Tursa 2020년 12월 29일
Something like this:
try
fn = x+y+z; %The function is just a representation. I am actually performing a Finite Element Analaysis
ans = func(fn); %The equation is solved using an external function
catch
continue
end
  댓글 수: 1
Gekkouga
Gekkouga 2020년 12월 29일
Thanks. It worked. I was stuck with this for days.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 12월 29일
If func() is creating an error then use try/catch around the call to func()
  댓글 수: 1
Gekkouga
Gekkouga 2020년 12월 29일
Thanks. try/catch worked.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by