Path: news.mathworks.com!newsfeed-00.mathworks.com!panix!bloom-beacon.mit.edu!llnews!53ab2750!not-for-mail
Newsgroups: comp.soft-sys.matlab
Subject: Re: go to statment in matlab
References: <f9f08u$qqp$1@fred.mathworks.com> <fa61hd$so$1@fred.mathworks.com> <fa64gs$d9m$1@canopus.cc.umanitoba.ca> <fa6c94$70h$1@fred.mathworks.com> <fa77b3$3pt$1@canopus.cc.umanitoba.ca> <fae87n$651$1@fred.mathworks.com>
From: Peter Boettcher <boettcher@ll.mit.edu>
Message-ID: <muyveb92ajl.fsf@G99-Boettcher.llan.ll.mit.edu>
Organization: MIT Lincoln Laboratory
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/23.0.0 (gnu/linux)
Cancel-Lock: sha1:GE+WWWcXrpkfpASFwbug1zg2fYI=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Lines: 65
Date: Tue, 21 Aug 2007 11:05:02 -0400
NNTP-Posting-Host: 155.34.163.114
X-Complaints-To: news@ll.mit.edu
X-Trace: llnews 1187708412 155.34.163.114 (Tue, 21 Aug 2007 11:00:12 EDT)
NNTP-Posting-Date: Tue, 21 Aug 2007 11:00:12 EDT
Xref: news.mathworks.com comp.soft-sys.matlab:424795


"huda nawaf" <halmamory@yahoo.com> writes:

>  s=0;
>  try for K =1:20
>
>     s=s+1;
>  if K == 20
>     error('Opps0')
>   end
>   
>  %do_something_more;
>  g=10;
>  for i=1:10
>      s=s+g;
>  
> end
> end
>  return
> catch
>   if lasterror =='Opps0'
>
>   return
>   
>  rethrow(lasterror)
> end
> end
> but still there is aproblem .there are errors such:
> ??? Error using ==> ==
> Function '==' is not defined for values of class 'struct'.
>
> Error in ==> e:\MATLAB6p5\work\us1.m
> On line 18  ==>   if lasterror =='Opps0'
> thanks again
>
>

I don't understand why this needs try/catch.  How about this instead:


s=0;
for K =1:20
    s=s+1;
    if K == 20
        break;  % exit out of loop
    end

    %do_something_more;
    g=10;
    for i=1:10
        s=s+g;
    end
end


If you need to execute a cleanup condition, use an additional flag,
like

if K == 20
  this_was_a_messy_exit = 1;
  break;
end


-Peter