From: Simon Preston on
Hi,

Can somebody please suggest a way to do the following. I would like to be able to exit a loop prematurely by pressing a key on the keyboard. E.g. for the following function I would like to press, say, "q" to break the loop so the function returns foo and k however far it's got through the loop.

function [foo,k] = myFunc()
foo = 0;
for k = 1:1e10
foo = foo + randn;
% [break/return here upon user input]
end

Do I need to use callbacks? I came across this tutorial: http://blinkdagger.com/matlab/matlab-gui-tutorial-adding-keyboard-shortcuts-hotkeys-to-a-gui/2/ for doing similar in a GUI, but I'm still not clear what the simplest solution is.

Thanks for any help, S
From: Simon Preston on
"Simon Preston" <preston.simon+mathsworks(a)gmail.com> wrote in message <hpn8s5$27j$1(a)fred.mathworks.com>...
> Hi,
>
> Can somebody please suggest a way to do the following. I would like to be able to exit a loop prematurely by pressing a key on the keyboard. E.g. for the following function I would like to press, say, "q" to break the loop so the function returns foo and k however far it's got through the loop.
>
> function [foo,k] = myFunc()
> foo = 0;
> for k = 1:1e10
> foo = foo + randn;
> % [break/return here upon user input]
> end
>
> Do I need to use callbacks? I came across this tutorial: http://blinkdagger.com/matlab/matlab-gui-tutorial-adding-keyboard-shortcuts-hotkeys-to-a-gui/2/ for doing similar in a GUI, but I'm still not clear what the simplest solution is.
>
> Thanks for any help, S

Can anybody suggest anything for this question?

Thanks, S
From: Walter Roberson on
Simon Preston wrote:
> "Simon Preston" <preston.simon+mathsworks(a)gmail.com> wrote in message
> <hpn8s5$27j$1(a)fred.mathworks.com>...

>> Can somebody please suggest a way to do the following. I would like
>> to be able to exit a loop prematurely by pressing a key on the
>> keyboard.

Check out the Matlab File Exchange; it has some relevant
user-contributed utilities.