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:
break/return upon user input

Subject: break/return upon user input

From: Simon Preston

Date: 9 Apr, 2010 13:08:21

Message: 1 of 3

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

Subject: break/return upon user input

From: Simon Preston

Date: 19 Apr, 2010 17:00:40

Message: 2 of 3

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message <hpn8s5$27j$1@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

Subject: break/return upon user input

From: Walter Roberson

Date: 19 Apr, 2010 17:24:42

Message: 3 of 3

Simon Preston wrote:
> "Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message
> <hpn8s5$27j$1@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.

Tags for this Thread

No tags are associated with 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