Is there a command in Matab like the Pascal command 'KeyPressed'?
When running a program, I wonder whether it is possible to interrupt it
during execution, so that it is possible to change e.g. some parameters and
then to continue the execution.
In Pascal, this can be done by writing the program in the loop 'repeat
until KeyPressed'.
Can something analogue be done in Matlab?
Thank you very much in advance,
Kris Van de Rostyne
Kris.van...@fys.kuleuven.ac.be
That would be wonderful. It is not in the current version of Matlab,
but I hope Matlab sees this and implements it in the next version.
There is a place on their web site where you can suggest enhancements
such as this. http://www.mathworks.com
Matt Taylor
On 21 Sep 1998 12:32:18 GMT, "Kris Van de Rostyne"
MATLAB has a command called KEYBOARD that you can place in your M-file. Here
is a short description:
KEYBOARD Invoke keyboard from M-file.
KEYBOARD, when placed in an M-file, stops execution of the file
and gives control to the user's keyboard. The special status is
indicated by a K appearing before the prompt. Variables may be
examined or changed - all MATLAB commands are valid. The keyboard
mode is terminated by executing the command RETURN (i.e. typing
the six letters R-E-T-U-R-N and pressing the return key). Control
returns to the invoking M-file.
DBQUIT can also be used to get out of keyboard mode but in this case
the invoking M-file is terminated.
The keyboard mode is useful for debugging your M-files.
See also DBQUIT, DBSTOP, RETURN, INPUT.
Good luck!
-- Mili
Kris Van de Rostyne wrote in message
<01bde55b$e3f04180$4250...@ATF66.fys.kuleuven.ac.be>...
>Is there a command in Matab like the Pascal command 'KeyPressed'?
>When running a program, I wonder whether it is possible to interrupt it
>during execution, so that it is possible to change e.g. some parameters and
>then to continue the execution.
Hi Kris -
One way to do this would be via uicontrols. Try this simple example..
It's not exactly what you're after, but it may do the trick.
% make a uicontrol that will be used to interrupt some loop
g = 0;
f = figure;
b = uicontrol('style','push','string','g++','callback','g=g+1');
% now the loop
while g < 10
fprintf(1,'The variable g is now %i\n', g);
drawnow
end
Hope this helps!
Greg
%-------------------------------------------------------
% Greg Wolodkin The MathWorks
% gr...@mathworks.com 24 Prime Park Way
% (508) 647-7000, ext.7637 Natick, MA 01760-1500
% (508) 647-7012, FAX http://www.mathworks.com
I've done things like this before. The loop continues
until the user presses the P key to pause or the S key
to stop completely. I had a plot window open (hence the
reference to gcf, but I guess you could use the root
object also, i.e. replace gcf with 0).
k=[];
set(gcf,'keypress','k=get(gcf,''currentchar'');');
while 1
%do stuff here
if ~isempty(k)
if strcmp(k,'s'); break; end;
if strcmp(k,'p'); pause; k=[]; end;
end
end
Hope this helps.
Mark
Hi,
it looks like KEYBOARD command itself isn't enough to implement "repeat until
KeyPresed" loop. Unlike this loop KEYBOARD interrupts M-File in fixed
place and doesn't continue execution if there is no input. To implement this
loop one can use callback for arbitrary GUI control.
The fllowing is the simplest M-file for demo.
pflag=0;
sflag=0;
uicontrol('String','Pause','CallBack','pflag=1;');
while (~sflag)
%do smth
if (pflag)
keyboard;
pflag=0;
end
pause(0.0);
end
Regards,
Igor Kaufman
> Dear Kris,
>
> MATLAB has a command called KEYBOARD that you can place in your M-file. Here
> is a short description:
>
> KEYBOARD Invoke keyboard from M-file.
> KEYBOARD, when placed in an M-file, stops execution of the file
> and gives control to the user's keyboard. The special status is
> indicated by a K appearing before the prompt. Variables may be
> examined or changed - all MATLAB commands are valid. The keyboard
> mode is terminated by executing the command RETURN (i.e. typing
> the six letters R-E-T-U-R-N and pressing the return key). Control
> returns to the invoking M-file.
>
> DBQUIT can also be used to get out of keyboard mode but in this case
> the invoking M-file is terminated.
>
> The keyboard mode is useful for debugging your M-files.
>
> See also DBQUIT, DBSTOP, RETURN, INPUT.
>
> Good luck!
>
> -- Mili
>
> Kris Van de Rostyne wrote in message
> <01bde55b$e3f04180$4250...@ATF66.fys.kuleuven.ac.be>...
> >Hello,
> >
> >Is there a command in Matab like the Pascal command 'KeyPressed'?
> >When running a program, I wonder whether it is possible to interrupt it
> >during execution, so that it is possible to change e.g. some parameters and
> >then to continue the execution.
> >In Pascal, this can be done by writing the program in the loop 'repeat
> >until KeyPressed'.
> >Can something analogue be done in Matlab?
> >
> >Thank you very much in advance,
> >
> >Kris Van de Rostyne
> >Kris.van...@fys.kuleuven.ac.be
>
>
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
An example would be the following
% Causes UNIX standard input to read one character at a time rather than
line by line
setkeypress;
while getkey~=0 % a non-interrupt keyboard press
...
end
% Undos the damage cause by setkeypress.
% Not doing this will cause the prompt to act funny
resetkeypress;
_____
#include <stdlib.h>
#include <stdio.h>
#include <termios.h>
#include <string.h>
static struct termios stored;
void set_keypress(void)
{
struct termios newb;
tcgetattr(0,&stored);
memcpy(&newb,&stored,sizeof(struct termios));
/* Disable canonical mode, and set buffer size to 1 byte */
newb.c_lflag &= (~ICANON);
newb.c_cc[VTIME] = 0;
newb.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&newb);
return;
}
void reset_keypress(void)
{
tcsetattr(0,TCSANOW,&stored);
return;
}
int isready(int fd)
{
int rc;
fd_set fds;
struct timeval tv;
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0)
return -1;
return FD_ISSET(fd,&fds) ? 1 : 0;
}
int get_key(void)
{
int ch;
if (isready(0)) {
ch=getc(stdin);
} else {
ch=0;
}
return ch;
}
Mark Brown wrote:
> Kris Van de Rostyne wrote:
> >
> > Hello,
> >
> > Is there a command in Matab like the Pascal command 'KeyPressed'?
> > When running a program, I wonder whether it is possible to interrupt
> it
> > during execution, so that it is possible to change e.g. some
> parameters and
> > then to continue the execution.
> > In Pascal, this can be done by writing the program in the loop
> 'repeat
> > until KeyPressed'.
> > Can something analogue be done in Matlab?
> >
> > Thank you very much in advance,
> >
> > Kris Van de Rostyne
> > Kris.van...@fys.kuleuven.ac.be
>