http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218
MATLAB Central Newsreader  try catch /ctrlc behaviour
Feed for thread: try catch /ctrlc behaviour
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 01 Jun 2004 16:59:56 +0000
try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#173263
Guillaume
Hi, I couldn't find an answer to this when searching previous posts.<br>
<br>
In short, why don't I see 'Stop' if I press ctrlc while this is running?<br>
<br>
%<br>
try,<br>
while 1,<br>
disp('Running');<br>
pause(0.5);<br>
end,<br>
catch,<br>
disp('Stop');<br>
end<br>
%<br>
<br>
There must be something I don't understand about the try/catch behaviour.<br>
<br>
Thanks,<br>
<br>
Guillaume

Tue, 01 Jun 2004 13:29:44 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#173269
riccardo
Guillaume wrote:<br>
><br>
><br>
> Hi, I couldn't find an answer to this when searching previous<br>
> posts.<br>
><br>
> In short, why don't I see 'Stop' if I press ctrlc while this is<br>
> running?<br>
><br>
> %<br>
> try,<br>
> while 1,<br>
> disp('Running');<br>
> pause(0.5);<br>
> end,<br>
> catch,<br>
> disp('Stop');<br>
> end<br>
> %<br>
><br>
> There must be something I don't understand about the try/catch<br>
> behaviour.<br>
><br>
> Thanks,<br>
><br>
> Guillaume<br>
><br>
Hi Guillaume,<br>
save your script as an mscript or an mfunction file, run it and it<br>
works.<br>
HTH<br>
riccardo

Tue, 01 Jun 2004 17:52:02 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#173278
Guillaume
>>In short, why don't I see 'Stop' if I press ctrlc while this is<br>
>>running?<br>
>><br>
>>%<br>
>>try,<br>
>>while 1,<br>
>>disp('Running');<br>
>>pause(0.5);<br>
>>end,<br>
>>catch,<br>
>>disp('Stop');<br>
>>end<br>
>>%<br>
>><br>
>>There must be something I don't understand about the try/catch<br>
>>behaviour.<br>
>><br>
> <br>
> Hi Guillaume,<br>
> save your script as an mscript or an mfunction file, run it and it<br>
> works.<br>
> HTH<br>
> riccardo<br>
<br>
I just did that and no, it doesn't work (i.e. hitting ctrlc doesn't get <br>
'Stop' to display ). This is on a Windows XP computer with ver. 6.5.<br>
<br>
Instead, this is displayed:<br>
<br>
Error in ==> D:\MATLAB6p5\work\test.m<br>
On line 4 ==> pause(0.5);<br>
<br>
<br>
Guillaume

Tue, 01 Jun 2004 18:10:02 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#173288
Guillaume
Sorry, I answered my own question and this is quite frustrating. This is <br>
from Solution Number: 11BSWG.<br>
<br>
> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch block.<br>
> In MATLAB 6.5 (R13), the catch block will not execute.<br>
> <br>
> Solution:<br>
> <br>
> TRY/CATCH no longer catches CtrlC interruptions as of MATLAB 6.5 (R13). This change in behavior was intentional. There are no known workarounds for reverting to the previous behavior. <br>
<br>
So is there another way to get Matlab to, for example, pass intermediate <br>
results from a while loop, after hitting ctrlc, to a function output?<br>
<br>
e.g. running this function, hitting ctrlc and still get the value of t <br>
when I stopped it?<br>
%<br>
function t=test;<br>
t+0;<br>
while 1,<br>
t=t+1;<br>
end<br>
%<br>
<br>
I want to use this to get intermediate results from an optimization.<br>
<br>
Guillaume<br>
<br>
<br>
> Hi, I couldn't find an answer to this when searching previous posts.<br>
> <br>
> In short, why don't I see 'Stop' if I press ctrlc while this is running?<br>
> <br>
> %<br>
> try,<br>
> while 1,<br>
> disp('Running');<br>
> pause(0.5);<br>
> end,<br>
> catch,<br>
> disp('Stop');<br>
> end<br>
> %<br>
> <br>
> There must be something I don't understand about the try/catch behaviour.<br>
> <br>
> Thanks,<br>
> <br>
> Guillaume

Tue, 01 Jun 2004 15:17:08 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#173307
Steven Lord
Guillaume wrote:<br>
> Sorry, I answered my own question and this is quite frustrating. This<br>
> is from Solution Number: 11BSWG.<br>
><br>
>> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch<br>
>> block. In MATLAB 6.5 (R13), the catch block will not execute.<br>
>><br>
>> Solution:<br>
>><br>
>> TRY/CATCH no longer catches CtrlC interruptions as of MATLAB 6.5<br>
>> (R13). This change in behavior was intentional. There are no known<br>
>> workarounds for reverting to the previous behavior.<br>
><br>
> So is there another way to get Matlab to, for example, pass<br>
> intermediate results from a while loop, after hitting ctrlc, to a<br>
> function output?<br>
<br>
No, but perhaps there's another way to do what you really want to do ...<br>
<br>
> e.g. running this function, hitting ctrlc and still get the value of<br>
> t when I stopped it?<br>
> %<br>
> function t=test;<br>
> t+0;<br>
> while 1,<br>
> t=t+1;<br>
> end<br>
> %<br>
><br>
> I want to use this to get intermediate results from an optimization.<br>
<br>
and there is indeed another way to do what you want. As of version 2.3 of<br>
the Optimization Toolbox, you can specify an output function for use with<br>
your optimizer. By using this output function, you can specify when to stop<br>
the optimization interactively and perform any 'cleanup'type tasks you want<br>
before stopping. If you look at the Optimization Toolbox 2.3 Release Notes,<br>
you'll see a link to an output function example.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com

Fri, 05 Apr 2013 09:27:16 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#901596
Peeyush
"Steven Lord" <slord@mathworks.com> wrote in message <c9iknk$s3g$1@fred.mathworks.com>...<br>
> Guillaume wrote:<br>
> > Sorry, I answered my own question and this is quite frustrating. This<br>
> > is from Solution Number: 11BSWG.<br>
> ><br>
> >> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch<br>
> >> block. In MATLAB 6.5 (R13), the catch block will not execute.<br>
> >><br>
> >> Solution:<br>
> >><br>
> >> TRY/CATCH no longer catches CtrlC interruptions as of MATLAB 6.5<br>
> >> (R13). This change in behavior was intentional. There are no known<br>
> >> workarounds for reverting to the previous behavior.<br>
> ><br>
> > So is there another way to get Matlab to, for example, pass<br>
> > intermediate results from a while loop, after hitting ctrlc, to a<br>
> > function output?<br>
<br>
<br>
Continuing in the same vein, what if I want to stop the execution of a long loop before it's termination condition? and I still want to carry out some processing on a partially analyzed dataset? Currently, ctrlc breaks the loop and I lose the intermediate results.... <br>
<br>
I'm running Matlab 12 on a Mac.

Fri, 05 Apr 2013 09:49:07 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#901598
Nasser M. Abbasi
On 4/5/2013 4:27 AM, Peeyush wrote:<br>
<br>
><br>
> Continuing in the same vein, what if I want to stop the execution<br>
>of a long loop before it's termination condition? and I still want to carry<br>
>out some processing on a partially analyzed dataset? Currently, ctrlc breaks<br>
>the loop and I lose the intermediate results....<br>
><br>
> I'm running Matlab 12 on a Mac.<br>
><br>
<br>
You have to add your own logic. For example, Make a button and<br>
in the callback set a flag that the main loop checks on at the<br>
end of each iteration. When the main loop detects this flag is set to<br>
terminate, then it can cleanly terminate and do any other<br>
intermediate work.<br>
<br>
Nasser<br>

Fri, 05 Apr 2013 14:23:17 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#901616
Steven_Lord
<br>
<br>
"Nasser M. Abbasi" <nma@12000.org> wrote in message <br>
news:kjm6mi$tn5$1@speranza.aioe.org...<br>
> On 4/5/2013 4:27 AM, Peeyush wrote:<br>
><br>
>><br>
>> Continuing in the same vein, what if I want to stop the execution<br>
>>of a long loop before it's termination condition? and I still want to <br>
>>carry<br>
>>out some processing on a partially analyzed dataset? Currently, ctrlc <br>
>>breaks<br>
>>the loop and I lose the intermediate results....<br>
>><br>
>> I'm running Matlab 12 on a Mac.<br>
>><br>
><br>
> You have to add your own logic. For example, Make a button and<br>
> in the callback set a flag that the main loop checks on at the<br>
> end of each iteration. When the main loop detects this flag is set to<br>
> terminate, then it can cleanly terminate and do any other<br>
> intermediate work.<br>
<br>
Having your program create an onCleanup object that saves the workspace as <br>
its cleanup function before entering the loop is another possibility.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Tue, 25 Feb 2014 21:17:11 +0000
Re: try catch /ctrlc behaviour
http://www.mathworks.com/matlabcentral/newsreader/view_thread/68218#918712
Brandon Kuczenski
"Steven_Lord" <slord@mathworks.com> wrote in message > <br>
> Having your program create an onCleanup object that saves the workspace as <br>
> its cleanup function before entering the loop is another possibility.<br>
> <br>
>  <br>
> Steve Lord<br>
> slord@mathworks.com<br>
> To contact Technical Support use the Contact Us link on <br>
> <a href="http://www.mathworks.com">http://www.mathworks.com</a> <br>
<br>
Steve,<br>
<br>
Thanks for this idea. I am trying to solve a similar problem as the OP I have a large structure that works as a database. I have a deep function that makes incremental changes to the database, but I want to throw out all the changes if there are any errors. I wrote the following wrapper function, which works if I encounter an error but fails under CtrlC. I can't understand how to use onCleanup because I can't find a way to pass the temporary variable into the onCleanup object's workspace:<br>
<br>
function do_import(foo)<br>
global DB<br>
dtmp=DB;<br>
try<br>
handle_import(foo);<br>
catch exception<br>
fprintf('failed!\n')<br>
DB=dtmp;<br>
throw(exception)<br>
end<br>
<br>
I could do 'DB=dtmp' in the onCleanup object if I could somehow make it aware of dtmp. Is there a way to do this?<br>
Thanks