Reputation: 7995
I have a loop in which I keep entering points into figure using ginput. I would like the loop to run until user presses a key, Heres what I have:
function enter_points()
f = figure();
axis([-1 1 -1 1]);
coorX = [];
coorY = [];
while 1
[x, y] = ginput(1);
coorX = [coorX x];
coorY = [coorY y];
waitforbuttonpress;
key = get(f,'CurrentCharacter');
if (key == 'e')
display('End of cycle.')
break;
else
display('Enter next point')
end
end
coor = [transpose(coorX) transpose(coorY)];
display(size(coor));
display(coor);
end
The problem with this code is, that I haveto press a key to continue entering points. Another problem is, that Matlab sometimes freezes when running this code (Im not sure it the code is the reason or something else is). And how to detect pressing "enter" instead of "e"? Thanks for any help
Upvotes: 2
Views: 14283
Reputation: 9313
bdecaf already gave you the simplest answer, but you could also try these couple of changes:
1) Change this:
[x, y] = ginput(1);
to this:
[x, y, key] = ginput(1);
ginput also returns the key you press and even which mouse button (1:LB, 2:RB or 3:MB).
2) Delete these lines:
waitforbuttonpress;
key = get(f,'CurrentCharacter');
With these changes your routine should work as intended. No pause between points, and exit when pressing [e].
Upvotes: 2
Reputation: 4732
Why don't you use the builtin:
[X,Y] = ginput gathers an unlimited number of points until the return key is pressed.
Upvotes: 3
Reputation: 8391
AFAIK, the general way to handle your problem in OOP and Event Oriented Programming is to generate a listener to a given event, in your case a keypress
-like event. When defining the listener, one usually passes a callback function to be called(-back) when the event is generated.
One may define listeners e.g. in matlab GUIs (reference). Nonetheless, I am not sure one can do that when the event is generated at the console level.
Upvotes: 3