Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
ignore any kind of errors in a loop?

Subject: ignore any kind of errors in a loop?

From: Per Sundqvist

Date: 4 May, 2011 09:13:04

Message: 1 of 4

I have a loop that calculates a lot of data based on datafiles it reads. Now it happens that the files sometimes gives very odd data, and it is unpredictible some times. In those cases I just want the loop to continue, ignoring the mathematical errors that come out from that specific file. I have say 600.000 files to go through so 5% errors doesn't matter. But it is really enoying if the loop stops at 12000 out of 600k, because of some random error. Is the there a "if any type of error -continue"?

Here is an example, but it does not work. It should skip j=5, what ever error occurs there.

try
    cc=1;nun=zeros(1,10);
    for j=1:10
        if j==5
            dum=eig(NaN); %just an example that would stop the loop. Replace with other.
            nun(cc)=eig(1);cc=cc+1;
        else
            nun(cc)=j*2;cc=cc+1;
        end
    end
catch exception
    throw(exception);
end
nun

Many thanks,
Per

Subject: ignore any kind of errors in a loop?

From: Steven_Lord

Date: 4 May, 2011 13:47:36

Message: 2 of 4



"Per Sundqvist" <per.sundqvist@live.com> wrote in message
news:ipr5b0$8t1$1@fred.mathworks.com...
> I have a loop that calculates a lot of data based on datafiles it reads.
> Now it happens that the files sometimes gives very odd data, and it is
> unpredictible some times. In those cases I just want the loop to continue,
> ignoring the mathematical errors that come out from that specific file. I
> have say 600.000 files to go through so 5% errors doesn't matter. But it
> is really enoying if the loop stops at 12000 out of 600k, because of some
> random error. Is the there a "if any type of error -continue"?
>
> Here is an example, but it does not work. It should skip j=5, what ever
> error occurs there.
>
> try cc=1;nun=zeros(1,10);
> for j=1:10
> if j==5
> dum=eig(NaN); %just an example that would stop the loop.
> Replace with other.
> nun(cc)=eig(1);cc=cc+1;
> else
> nun(cc)=j*2;cc=cc+1;
> end
> end
> catch exception
> throw(exception);

If you check the exception's identifier and decide that it's safe to proceed
(because the error you received is expected and manageable) you don't _have_
to THROW or RETHROW the exception.


clear a
try
    a = [1 1]+[1; 1];
catch exception
    if isequal(exception.identifier, 'MATLAB:dimagree')
        % This is the exception we expected
        fprintf('The addition failed because the matrix dimensions didn''t
agree.\n');
        a = [];
    else
        rethrow(exception);
    end
end
whos


The way you've written the code, MATLAB has already exited the loop when the
error occurs (since the CATCH is outside) so you can't continue with the
loop with j = 6 as written. You'd need:


cc=1;nun=zeros(1,10);
for j=1:10
    try
        if j==5
            dum=eig(NaN); %just an example that would stop the loop. Replace
with other.
            nun(cc)=eig(1);cc=cc+1;
% No need for a separate counter variable in this particular scenario; just
use j instead of cc.
% [Yes, I know, this is probably only a model of your actual problem where
you do need a counter variable.]
        else
            nun(cc)=j*2;cc=cc+1;
% Ditto.
        end
    catch exception
        if ~isAcceptableToContinue(exception)
            rethrow(exception);
        end
    end
end


For some appropriate definition of isAcceptableToContinue().

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: ignore any kind of errors in a loop?

From: Michelle

Date: 19 Jul, 2012 21:06:13

Message: 3 of 4

I'm having a very similar problem but in my case instead of a loop I have:

arrayfun(@(d) lambdad_sampler_rd(d,Y,X,lambda_ini), d)

d is a vector of size 90653 and inside my function lambdad_sampler_rd, I call the function slicesample from matlab. For some values of d, slice sampler gives me an error message and it stops right there. I want to ignore those d's (and save them, so I would know which ones failed) and keep evaluating the function for the other ones.

Is it possible to do it?

If it helps, the error I get from slice sampler is

??? Error using ==> slicesample>checkFunErrs at 231
PDF returned a NaN or Inf.

Error in ==> slicesample at 98
    checkFunErrs('pdf',pdf,initial); % error check for pdf function






"Steven_Lord" <slord@mathworks.com> wrote in message <iprldo$ff9$1@fred.mathworks.com>...
>
>
> "Per Sundqvist" <per.sundqvist@live.com> wrote in message
> news:ipr5b0$8t1$1@fred.mathworks.com...
> > I have a loop that calculates a lot of data based on datafiles it reads.
> > Now it happens that the files sometimes gives very odd data, and it is
> > unpredictible some times. In those cases I just want the loop to continue,
> > ignoring the mathematical errors that come out from that specific file. I
> > have say 600.000 files to go through so 5% errors doesn't matter. But it
> > is really enoying if the loop stops at 12000 out of 600k, because of some
> > random error. Is the there a "if any type of error -continue"?
> >
> > Here is an example, but it does not work. It should skip j=5, what ever
> > error occurs there.
> >
> > try cc=1;nun=zeros(1,10);
> > for j=1:10
> > if j==5
> > dum=eig(NaN); %just an example that would stop the loop.
> > Replace with other.
> > nun(cc)=eig(1);cc=cc+1;
> > else
> > nun(cc)=j*2;cc=cc+1;
> > end
> > end
> > catch exception
> > throw(exception);
>
> If you check the exception's identifier and decide that it's safe to proceed
> (because the error you received is expected and manageable) you don't _have_
> to THROW or RETHROW the exception.
>
>
> clear a
> try
> a = [1 1]+[1; 1];
> catch exception
> if isequal(exception.identifier, 'MATLAB:dimagree')
> % This is the exception we expected
> fprintf('The addition failed because the matrix dimensions didn''t
> agree.\n');
> a = [];
> else
> rethrow(exception);
> end
> end
> whos
>
>
> The way you've written the code, MATLAB has already exited the loop when the
> error occurs (since the CATCH is outside) so you can't continue with the
> loop with j = 6 as written. You'd need:
>
>
> cc=1;nun=zeros(1,10);
> for j=1:10
> try
> if j==5
> dum=eig(NaN); %just an example that would stop the loop. Replace
> with other.
> nun(cc)=eig(1);cc=cc+1;
> % No need for a separate counter variable in this particular scenario; just
> use j instead of cc.
> % [Yes, I know, this is probably only a model of your actual problem where
> you do need a counter variable.]
> else
> nun(cc)=j*2;cc=cc+1;
> % Ditto.
> end
> catch exception
> if ~isAcceptableToContinue(exception)
> rethrow(exception);
> end
> end
> end
>
>
> For some appropriate definition of isAcceptableToContinue().
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: ignore any kind of errors in a loop?

From: Bruno Luong

Date: 19 Jul, 2012 21:41:11

Message: 4 of 4

"Michelle " <michellemirandaest@gmail.com> wrote in message <ju9ss5$g4r$1@newscl01ah.mathworks.com>...
> I'm having a very similar problem but in my case instead of a loop I have:
>
> arrayfun(@(d) lambdad_sampler_rd(d,Y,X,lambda_ini), d)
>
> d is a vector of size 90653 and inside my function lambdad_sampler_rd, I call the function slicesample from matlab. For some values of d, slice sampler gives me an error message and it stops right there. I want to ignore those d's (and save them, so I would know which ones failed) and keep evaluating the function for the other ones.
>
> Is it possible to do it?

Please use for-loop. Do not stuck with ARRAYFUN, the most overrated command in MATLAB.

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us