Ginput not recognising the enter key

조회 수: 17 (최근 30일)
BC
BC 2021년 5월 5일
댓글: Adam Danz 2021년 5월 5일
I'm using ginput to plot coordinates on an image, and saving these coordinates in a variable to use elswhere. I've tried to make it so if I make a mistake when clicking points on the image, I can press the backspace key to delete the last point that was clicked.
Pressing any key other than a left click or the backspace key should lead to the image closing and the while loop ending, but if I press the Enter key, ginput doesn't recognise it as a key, so it leads to an error where "button" can't be indexed. Other keys seem to be recognised and work properly.
Not sure why it's happening, hoping it might be something obvious, any help would be much appreciated. Any other critisisms of the code are also welcome, I'm still learning!
ant = imread("ant.jpg");
imshow(ant) % display image
hold on % hold axes for plotting
% enable ONLY zooming and panning, avoid data tips
ax = gca;
ax.Interactions = [panInteraction zoomInteraction];
enableDefaultInteractivity(ax) % enable interactions
% plot coordinates on image and save in variable
n = 0;
while true
[x, y, button] = ginput(1);
% if button isn't left click or a backspace key, close image and end
if button(1) ~= 1 && button(1) ~= 8;
disp(button(1))
disp("No left mouse click or backspace, closing figure")
close all, break, end;
if ~isempty(x) && button == 1 % if left mouse click, save coordinate + plot on image
n = n+1;
x_coord(n,:) = x(1); % save x coordinate
y_coord(n,:) = y(1); % save y coordinate
p(n) = plot(x, y, "yo","markersize",10); % plot coordinates as you draw
% else if backspace key pressed, delete the last coordinate
elseif ~isempty(x) && button == 8;
if n>1
delete(p(n)); % Remove last plotted point
p(n) = [];
x_coord(end) = ["Deleted"]; % appears as NaN, text irrelevant
y_coord(end) = ["Deleted"];
end
else
end
end
end

채택된 답변

Adam Danz
Adam Danz 2021년 5월 5일
> if I press the Enter key, ginput doesn't recognise it as a key
From the documentation, "Press the Return key to stop before all n points are selected."
"Enter" is the same as "Return". When Enter/Return is pressed, 'button' will be empty ([]).
This is where your 'else' comes in...
if ~isempty(x) && button == 1 % if left mouse click, save coordinate + plot on image
.
.
.
% else if backspace key pressed, delete the last coordinate
elseif ~isempty(x) && button == 8;
.
.
.
else % <------ EMPTY
end
  댓글 수: 4
BC
BC 2021년 5월 5일
Thanks for the advice; I'm going to restructure it tomorrow with your example.
In this case I check the same image twice, and I'm often testing things out in other scripts, so I thought using close all would declutter my workspace a bit in case I've left something up by accident! But if I were to close specific ones, would I need to name the figure first? E.g.:
ant_figure = figure;
imshow(ant)
close ant_figure;
Adam Danz
Adam Danz 2021년 5월 5일
Yes, you would need the figure handle as you have demonstrated. You can also close a vector of handles if you have multiple figures.
f1 = figure();
f2 = figure();
f3 = figure();
close([f1,f2,f3])

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by