Ginput not recognising the enter key

17 次查看(过去 30 天)
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