Discussion:
HELP: Repeat Until Keypressed with MATLAB
(too old to reply)
GS
2003-11-24 16:20:37 UTC
Permalink
Does anyone know a solution for the following problem?

I need to write a command in MATLAB to obtain a "repeat until
keypressed" loop. In other terms, I need to execute a series of
commands (loop) until a key (any or specific key) is pressed on the
keyboard.
I have tried to make this by using various combinations of while,
break, pause... but unsuccessfuly.
Someone gave me help on this subject, but on figure object, but I do
not deal with figure object, I deal with a loop of command.

How can I do?

Thank you very much.
G. S.
us
2003-11-24 18:53:24 UTC
Permalink
GS:
<SNIP repeats his poster: i want to use a keypress to break out of
a loop>
Post by GS
Someone gave me help on this subject, but on figure object, but I
do not deal with figure object, I deal with a loop of command.

well, since i was the <one>, i'd like to quickly elaborate on
this:
1) currently, there is no other way.
2) why not just use a dummy figure. look at the code snippet below.
it opens a figure, takes the focus. the loop/computation itself is
<object-free> ...
just a thought
us

<<< CODE BEG >>>
% simulate a gui
f=figure;

% make sure it has the focus!
% otherwise you'd have to restart ML
disp('press mouse button to start');
waitforbuttonpress;
disp('press key <a> to stop');

% simulate a loop
v=0;
while true
% ... some computation, eg,
v=v+1;
% BREAK POINT BEG
pause(0);
if get(f,'currentkey') == 'a'
disp('stopped');
break;
end
% BREAK POINT END
% ... more computation
end
disp(v);
<<< CODE END >>>
Dan Hensley
2003-11-24 19:19:43 UTC
Permalink
Post by us
<SNIP repeats his poster: i want to use a keypress to break out of
a loop>
Post by GS
Someone gave me help on this subject, but on figure object, but I
do not deal with figure object, I deal with a loop of command.
well, since i was the <one>, i'd like to quickly elaborate on
1) currently, there is no other way.
2) why not just use a dummy figure. look at the code snippet below.
it opens a figure, takes the focus. the loop/computation itself is
<object-free> ...
just a thought
us
<<< CODE BEG >>>
% simulate a gui
f=figure;
% make sure it has the focus!
You could force it to have focus using

f=figure('Windowstyle','modal');

Of course, you would need to make sure you delete the figure when done
Post by us
% otherwise you'd have to restart ML
disp('press mouse button to start');
waitforbuttonpress;
disp('press key <a> to stop');
% simulate a loop
v=0;
while true
% ... some computation, eg,
v=v+1;
% BREAK POINT BEG
pause(0);
if get(f,'currentkey') == 'a'
Or say

if ~ishandle(f) | get(f,'currentkey') == 'a'
Post by us
disp('stopped');
break;
end
% BREAK POINT END
% ... more computation
end
disp(v);
<<< CODE END >>>
if ishandle(f), delete(f);, end

Dan
GS
2003-11-25 10:10:43 UTC
Permalink
Thank you very much, "us" and Dan.
G. S.
Post by Dan Hensley
Post by us
<SNIP repeats his poster: i want to use a keypress to break out of
a loop>
Post by GS
Someone gave me help on this subject, but on figure object, but I
do not deal with figure object, I deal with a loop of command.
well, since i was the <one>, i'd like to quickly elaborate on
1) currently, there is no other way.
2) why not just use a dummy figure. look at the code snippet below.
it opens a figure, takes the focus. the loop/computation itself is
<object-free> ...
just a thought
us
<<< CODE BEG >>>
% simulate a gui
f=figure;
% make sure it has the focus!
You could force it to have focus using
f=figure('Windowstyle','modal');
Of course, you would need to make sure you delete the figure when done
Post by us
% otherwise you'd have to restart ML
disp('press mouse button to start');
waitforbuttonpress;
disp('press key <a> to stop');
% simulate a loop
v=0;
while true
% ... some computation, eg,
v=v+1;
% BREAK POINT BEG
pause(0);
if get(f,'currentkey') == 'a'
Or say
if ~ishandle(f) | get(f,'currentkey') == 'a'
Post by us
disp('stopped');
break;
end
% BREAK POINT END
% ... more computation
end
disp(v);
<<< CODE END >>>
if ishandle(f), delete(f);, end
Dan
Loading...