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:
try catch /ctrl-c behaviour

Subject: try catch /ctrl-c behaviour

From: Guillaume

Date: 1 Jun, 2004 16:59:56

Message: 1 of 9

Hi, I couldn't find an answer to this when searching previous posts.

In short, why don't I see 'Stop' if I press ctrl-c while this is running?

%
try,
   while 1,
       disp('Running');
       pause(0.5);
   end,
catch,
   disp('Stop');
end
%

There must be something I don't understand about the try/catch behaviour.

Thanks,

Guillaume

Subject: try catch /ctrl-c behaviour

From: riccardo

Date: 1 Jun, 2004 13:29:44

Message: 2 of 9

Guillaume wrote:
>
>
> Hi, I couldn't find an answer to this when searching previous
> posts.
>
> In short, why don't I see 'Stop' if I press ctrl-c while this is
> running?
>
> %
> try,
> while 1,
> disp('Running');
> pause(0.5);
> end,
> catch,
> disp('Stop');
> end
> %
>
> There must be something I don't understand about the try/catch
> behaviour.
>
> Thanks,
>
> Guillaume
>
Hi Guillaume,
save your script as an m-script or an m-function file, run it and it
works.
HTH
riccardo

Subject: try catch /ctrl-c behaviour

From: Guillaume

Date: 1 Jun, 2004 17:52:02

Message: 3 of 9

>>In short, why don't I see 'Stop' if I press ctrl-c while this is
>>running?
>>
>>%
>>try,
>>while 1,
>>disp('Running');
>>pause(0.5);
>>end,
>>catch,
>>disp('Stop');
>>end
>>%
>>
>>There must be something I don't understand about the try/catch
>>behaviour.
>>
>
> Hi Guillaume,
> save your script as an m-script or an m-function file, run it and it
> works.
> HTH
> riccardo

I just did that and no, it doesn't work (i.e. hitting ctrl-c doesn't get
'Stop' to display ). This is on a Windows XP computer with ver. 6.5.

Instead, this is displayed:

Error in ==> D:\MATLAB6p5\work\test.m
On line 4 ==> pause(0.5);


Guillaume

Subject: try catch /ctrl-c behaviour

From: Guillaume

Date: 1 Jun, 2004 18:10:02

Message: 4 of 9

Sorry, I answered my own question and this is quite frustrating. This is
from Solution Number: 1-1BSWG.

> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch block.
> In MATLAB 6.5 (R13), the catch block will not execute.
>
> Solution:
>
> TRY/CATCH no longer catches Ctrl-C interruptions as of MATLAB 6.5 (R13). This change in behavior was intentional. There are no known workarounds for reverting to the previous behavior.

So is there another way to get Matlab to, for example, pass intermediate
results from a while loop, after hitting ctrl-c, to a function output?

e.g. running this function, hitting ctrl-c and still get the value of t
when I stopped it?
%
function t=test;
t+0;
while 1,
        t=t+1;
end
%

I want to use this to get intermediate results from an optimization.

Guillaume


> Hi, I couldn't find an answer to this when searching previous posts.
>
> In short, why don't I see 'Stop' if I press ctrl-c while this is running?
>
> %
> try,
> while 1,
> disp('Running');
> pause(0.5);
> end,
> catch,
> disp('Stop');
> end
> %
>
> There must be something I don't understand about the try/catch behaviour.
>
> Thanks,
>
> Guillaume

Subject: try catch /ctrl-c behaviour

From: Steven Lord

Date: 1 Jun, 2004 15:17:08

Message: 5 of 9

Guillaume wrote:
> Sorry, I answered my own question and this is quite frustrating. This
> is from Solution Number: 1-1BSWG.
>
>> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch
>> block. In MATLAB 6.5 (R13), the catch block will not execute.
>>
>> Solution:
>>
>> TRY/CATCH no longer catches Ctrl-C interruptions as of MATLAB 6.5
>> (R13). This change in behavior was intentional. There are no known
>> workarounds for reverting to the previous behavior.
>
> So is there another way to get Matlab to, for example, pass
> intermediate results from a while loop, after hitting ctrl-c, to a
> function output?

No, but perhaps there's another way to do what you really want to do ...

> e.g. running this function, hitting ctrl-c and still get the value of
> t when I stopped it?
> %
> function t=test;
> t+0;
> while 1,
> t=t+1;
> end
> %
>
> I want to use this to get intermediate results from an optimization.

and there is indeed another way to do what you want. As of version 2.3 of
the Optimization Toolbox, you can specify an output function for use with
your optimizer. By using this output function, you can specify when to stop
the optimization interactively and perform any 'cleanup'-type tasks you want
before stopping. If you look at the Optimization Toolbox 2.3 Release Notes,
you'll see a link to an output function example.

--
Steve Lord
slord@mathworks.com

Subject: try catch /ctrl-c behaviour

From: Peeyush

Date: 5 Apr, 2013 09:27:16

Message: 6 of 9

"Steven Lord" <slord@mathworks.com> wrote in message <c9iknk$s3g$1@fred.mathworks.com>...
> Guillaume wrote:
> > Sorry, I answered my own question and this is quite frustrating. This
> > is from Solution Number: 1-1BSWG.
> >
> >> In MATLAB 6.1 (R12.1), ^c will break the loop and execute the catch
> >> block. In MATLAB 6.5 (R13), the catch block will not execute.
> >>
> >> Solution:
> >>
> >> TRY/CATCH no longer catches Ctrl-C interruptions as of MATLAB 6.5
> >> (R13). This change in behavior was intentional. There are no known
> >> workarounds for reverting to the previous behavior.
> >
> > So is there another way to get Matlab to, for example, pass
> > intermediate results from a while loop, after hitting ctrl-c, to a
> > function output?


 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, ctrl-c breaks the loop and I lose the intermediate results....

I'm running Matlab 12 on a Mac.

Subject: try catch /ctrl-c behaviour

From: Nasser M. Abbasi

Date: 5 Apr, 2013 09:49:07

Message: 7 of 9

On 4/5/2013 4:27 AM, Peeyush wrote:

>
> 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, ctrl-c breaks
>the loop and I lose the intermediate results....
>
> I'm running Matlab 12 on a Mac.
>

You have to add your own logic. For example, Make a button and
in the callback set a flag that the main loop checks on at the
end of each iteration. When the main loop detects this flag is set to
terminate, then it can cleanly terminate and do any other
intermediate work.

--Nasser
  

Subject: try catch /ctrl-c behaviour

From: Steven_Lord

Date: 5 Apr, 2013 14:23:17

Message: 8 of 9



"Nasser M. Abbasi" <nma@12000.org> wrote in message
news:kjm6mi$tn5$1@speranza.aioe.org...
> On 4/5/2013 4:27 AM, Peeyush wrote:
>
>>
>> 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, ctrl-c
>>breaks
>>the loop and I lose the intermediate results....
>>
>> I'm running Matlab 12 on a Mac.
>>
>
> You have to add your own logic. For example, Make a button and
> in the callback set a flag that the main loop checks on at the
> end of each iteration. When the main loop detects this flag is set to
> terminate, then it can cleanly terminate and do any other
> intermediate work.

Having your program create an onCleanup object that saves the workspace as
its cleanup function before entering the loop is another possibility.

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

Subject: try catch /ctrl-c behaviour

From: Brandon Kuczenski

Date: 25 Feb, 2014 21:17:11

Message: 9 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message >
> Having your program create an onCleanup object that saves the workspace as
> its cleanup function before entering the loop is another possibility.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Steve,

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 Ctrl-C. 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:

function do_import(foo)
global DB
dtmp=DB;
try
  handle_import(foo);
catch exception
  fprintf('failed!\n')
  DB=dtmp;
  throw(exception)
end

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

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