2

I have this code in MATLAB:

for i = 1: n
   a = randi([0 500]);
   function (a);
end

When there is error during the execution of function(a) in iteration i=k the program stops. Is there any way to make the program repeat the same iteration (when there is an error) with a new value of a and continue the execution?

2
  • 1
    The easiest way is to rewrite function so that it doesn't produce errors. Commented Apr 29, 2016 at 16:48
  • 3
    You don't. Other options include using a while loop with an incrementing counter based on the success of function or a try/catch statement. Commented Apr 29, 2016 at 16:51

2 Answers 2

2

The solution to your problem is pretty simple. Just use try, catch.

For loop that calls function

for i=1:3
    a=randi([0 500]);
    try
        myfunction(a); %Statements that may throw an error
    catch
        %Code that is executed if myfunction throws error
    end
    disp(i) %Proves that the loop continuous if myfunction throws an error
end

Function

function b = myfunction(a)
    b=a;
    error('Error!!!') %Function throws error every time it gets called
end

Output without try, catch

Error using myfunction (line 3)
Error!!!

Error in For_Error (line 6)
    myfunction(a); %Statements that may throw an error

Output with try, catch

1

2

3
Sign up to request clarification or add additional context in comments.

3 Comments

A try/catch block that is this indiscriminate is not a good idea. I highly recommend only catching specific errors, as defined by function, and rethrowing ones that do not match.
@user3288977 You do not have to write something into the catch block and depending on the application that could be totally okay. It is difficult to suggest something without knowing what your code is supposed to do. One possibility would be to create an error message to the user that explains what the cause of the exception might be (see try, catch documentation).
What I understand is that try/catch skips the iteration where there is an error and continue the loop... But I want to re-execute the same iteration where the error occurs... I hope the idea is clear
1

I think Kaspar answer is not exactly answering your question, user3717023. In Kaspar solution iteration is not repeated but simply skipped (like when using continue).

Proposed solution

If you want MATLAB to repeat iteration until myfunction() is completed successfully use while. Look at this this:

for ii = 1:30
   disp(ii)

   out = 0;
   while(~out)
       disp('Attempt ...')

       try
           out = myfunction(some_arguments);
       catch
           disp('F****ck!')
       end

       pause(1)
   end

   disp('OK !')           
end

If myfunction returns its output (which will happen if there was no error) it finishes while loop. Lines with disp added for self-descriptivity.

Line with pause added for neat output when running example.

Example

Run code above with below example of myfunction() to check how this solution works:

function out = myfunction(x)    
    a = randi(2,1,1) - 1;   % a = 0 or a = 1
    if a==0
        error
    else
        out = magic(3);
    end
end

Example output:

ii =

     1

Attempt ...
F****ck!
Attempt ...
OK !

ii =

     2

Attempt ...
OK !

ii =

     3

Attempt ...
F****ck!
Attempt ...
F****ck!
Attempt ...
F****ck!
Attempt ...
F****ck!
Attempt ...
OK !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.