How to avoid ginput from becoming unresponsive to keyboard keys?

9 次查看(过去 30 天)
I have written a simple function to keep ginputting and plotting points on the current axes, and deleting the last clicked point when the Backspace key is pressed, please see below. The while loop (normally) stops when I hit Enter key, which should return an empty value for [ix]
The code generally worked but depending on the number of points I define before hitting Backspace, the keyboard becomes unresponsive, and the Backspace key (or Enter key) does nothing (only mouse clicking works when that happens). Would anyone have an idea of what the issue could be please?
xpol=[]; ypol=[]; ix=0; p=0;
k=1;
while ~isempty(ix)
[ix,iy,key] = ginput(1); disp(key)
if ~isempty(key) && key~=8
xpol(k) = ix;
ypol(k) = iy;
p(k) = plot(xpol,ypol,'o-k','MarkerFaceColor','m','LineWidth',2);
k = k+1;
elseif ~isempty(key) && key==8
if k>1 % Make sure there is at least one point
k = k-1;
delete(p(k))
p(k) = [];
xpol(k) = [];
ypol(k) = [];
end
end
end
  3 个评论
Rafa
Rafa 2013-8-22
编辑:Rafa 2013-8-22
Hi, thanks for replying. If you create a hypothetical pseudo-colour figure:
figure; pcolor(ones(50,50)); hold on
and run the entire peace of code I posted before, you will be prompted to click on points on the plot until you hit the Enter key. Each time you click (or press any key other than Backspace or Enter), variables (xpol,ypol) should update, and the point should be added to the figure. If you press Backspace, the last defined point should be erased from vectors (xpol,ypol), and also from the plot.
When I for example clicked 3 times on the graphic to define and plot 3 points on it, and then pressed Backspace 3 times to erase them, it worked fine. However, when I ran the code and clicked 4 times, and tried to press Backspace after the fourth point was created, the keyboard became unresponsive. No key from the keyboard would work, only the mouse click.
When I defined 5 points, I managed to erase one, but then the keyboard became unresponsive again. When I defined 6, it became unresponsive after erasing two. And so on. (After describing this I suspect there might be a bug in my code, but I could not find it so far).
Thanks and I hope my explanation is not too confusing...
Yelena Bagdasarova
Yelena Bagdasarova 2018-11-28
编辑:Yelena Bagdasarova 2018-11-28
You know, I'm having major issues using the ginput function as well.
I'm running a R2018a install with updates on Linux. Ubuntu 18.04, and on an Asus VivoBook S laptop. I suspect it has something to do with Java, but I'm not knowlegable in that arena.
My I've transfered many of my old scipts using ginput onto this new computer/install, but I find that within the scripts (which are very busy creating new figures and plots) the ginput() function either is unresponsive to the return key and now that even with a call like ginput(2), it does not register the clicks and won't move on.
I've provided the first few lines of a function which uses ginput that I have this issue with. For this function, the figure does not come to the forefront while waiting for my input. So I have to find it among the previously plotted figures. The cursor is responsive and shows the selection cross but it does not go on after I've selected the said number of points.
Update: The function call is in a for loop, so it is called several times. When I put "drawnow" in front of ginput, it works the first time, but subsequent calls now call the ginput onto the wrong figure! I have a suspicion things just can't keep up.
Another Update: It seems that adding "pause(2)" before ginput works to have everything catch up before ginput is called.
function [freq,signal_Vmon,signal_Imon,signal_probe,pctVmon,pctImon,pctprobe]=FFTPlot(time, Vmon, Imon, probe,legendstring, figtitle,fig1title,fig2title)
pp = [1426 1 1855 1001]; %size of dell monitor
figure('Position', pp);
set(gcf,'DefaultAxesFontSize',15)
subplot(3,2,[1 2])
hold on
plot(time,Vmon)
plot(time,Imon)
if (not(isempty(probe)))
plot(time,probe,'r')
end
legend(legendstring)
%FFT
ind1 = 1;
ind2 = length(Vmon);
[x,y] = ginput(2);
ind1 = find(time > x(1),1,'first');
ind2 = find(time > x(2),1,'first');
...
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by