how to skip an error inside a loop?

조회 수: 9 (최근 30일)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019년 8월 15일
편집: Zeynab Mousavikhamene 2019년 8월 16일
I have a loop which reads from a structure. The pattern might change in the loop. Sometimes I need to call the element that I want by this structure: 1) data.timepoints(6).cells{89, 1}{2, 1}{2, 1}(3) and sometimes this strucutre gives error: "Brace indexing is not supported for variables of this type." so that I have to change to the: 2) data.timepoints(6).cells{90, 1}{2, 1} (3)
Is there a way to add "if conditional": if you get error with the 1st arrangement do the second arrangement.

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 15일
try
x = data.timepoints(6).cells{89, 1}{2, 1}{2, 1}(3);
catch ME
try
x = data.timepoints(6).cells{90, 1}{2, 1}(3);
catch MEME
%neither worked
end
end

추가 답변 (1개)

Michael
Michael 2019년 8월 15일
Hello,
you can use a try-catch statement. However, I think it is better to add an if-condition that checks the cells.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by