What is a try catch loop? try catch means branching in case of errors, but there is no loop. What is "the main code" and what might "between the loop" mean? For "between" there need to be at least 2 limits, but "between the loop" is only one.
"Between the bag there sat a man."
Please post some code, which demonstrates, what you are asking for. Prefer to edit the question instead of adding this information in a comment.
What does "how the structure is suggested" mean? If you are interested in assistance to solve your problem, it is useful to answer the questions for clarifications.
@farzad: You can be more clear: Simply explain, which problem you want to solve.
In this code:
for j = 1:10
try
... your code...
catch
warning('found error in %dth file', j);
continue
end
end
you do not have to write any code twice. The first comment (green line) is a placeholder for your code, and the second comment is some other code, which is executed only, if the first one ran without an error. An alternative version of the above code:
for j = 1:10
try
... your code...
... your code, if the above statement succeed
catch
warning('found error in %dth file', j);
end
end
So do you see now, that you do not write anything twice here?
You still did not mention, which problem you want to solve and you assume that anything must be written twice. My questions for clarifications are not answered yet.
You are not a newcomer in this forum, but please let me ask you to read this again: How to ask a good question
The code is not an improvement, but it does something else. As soon as you take the time to answer my questions for clarifications and explain, what you need, I can tell you, which of these two different methods is matching.
for j = 1:10
try
... your code...
catch
warning('found error in %dth file', j);
continue
end
... your code, if "try" codes succeed
end
This code calls te function "your code" 10 times and proceeds to the next iteration in case of an error.
My code repeats the call of "executeTheCode" until it is successful.
Currently all I can do is guessing, because I do not understand your question. Neither "a line to indicate the main code between the try catch loop to avoide rewriting it" nor "you will see how the structure is suggested" reveals, what you want to achieve.
By using Jan's answer which was posted 19 hours ago.
It only calls your code once, exactly where the rather explict executeTheCode is written.
"yourcode is repeated twice"
Nope. What you are commenting on is not Jan's answer, but some code that was copied from another answer which you linked to. That code has nothing to do with Jan's answer, nor does it work in the same way, or even do the same thing.
Jan already explained that : "This code calls the function "your code" 10 times and proceeds to the next iteration in case of an error." The part after the try-catch will only run if there is no error on that iteration.
It has nothing to do with Jan's answer, nor does it really help you with your original question.
"This code calls the function "your code" 10 times and proceeds to the next iteration in case of an error."
Actually this is what I need to do : I need to run all the iterations possible except the one that gives error.
the only thing I do not want to do is : not to put the same code twice.
Why ? because I have like 10 sets of runs, that read from some excel files and I do the same type of calculation on each of them. while some of these files might be corrupted, I want the code continue with the files that have not corurpted numbers inside them ( I don't know the file problem type so I don't want to use if) and then later after the whole analysis is done I will deal with the corrupted ones
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
5 Comments
Jan (view profile)
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/469194-how-not-to-repeat-the-whole-code-after-the-try-catch-loop#comment_719039
farzad (view profile)
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/469194-how-not-to-repeat-the-whole-code-after-the-try-catch-loop#comment_719046
Jan (view profile)
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/469194-how-not-to-repeat-the-whole-code-after-the-try-catch-loop#comment_719063
farzad (view profile)
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/469194-how-not-to-repeat-the-whole-code-after-the-try-catch-loop#comment_719299
Jan (view profile)
Direct link to this comment
https://nl.mathworks.com/matlabcentral/answers/469194-how-not-to-repeat-the-whole-code-after-the-try-catch-loop#comment_719321
Sign in to comment.