|
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
|