Question:

Matlab error by passing?

by  |  earlier

0 LIKES UnLike

im scanning a file with a model file, but some data in the file is not properly formatted. So using a while loop, the program runs fine until it hits the "corrupted" part and retunrs an errror message.

since the improper parts are random in this large set of data, and I do not need to scan these parts either, is there any way to bypass the error in matlab?

I tried in debuging to click all never stop if errors ... but the error still terminates the program.

 Tags:

   Report

2 ANSWERS


  1. Simplest way is to use try-catch. Place your code that can generate error like this:

    try

    error('Fail! Error!') %will always generate error

    catch e

    disp('There was an error but I will still continue')

    disp(e.message)

    end

    If you are running your code in loop, you can use command 'continue' in catch, this will skip rest of the code in your loop and starts new loop

    for ii = 1:4

    try

    if ii == 2

    error('error')

    end

    catch

    disp('there was an error')

    continue

    end

    disp(ii)

    end

    This would print:

    1

    2

    there was an error

    4


  2. You'd need to find or create a reasonably robust parser to skip over an arbitrary amount of arbitrary data simply by "recognising" it as garbage.

    One thing to do is to formulate your parser using regular expressions to match your data and go a character (or whatever your smallest data object is) at a time until you find your first object which doesn't match any regex.  Then what you need to do is "look ahead" far enough so that you can regain a footing on the context of the data you do want to be parsing as you traverse and ignore the bad data.

    You can re-use most of the code, only the regex's need to change and they can be parameterised.

    Or, instead of making one yourself, you could find a parser generator and have that create the parser according to your data structure.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.