Effacer les filtres
Effacer les filtres

Ginput not recognising the enter key

16 vues (au cours des 30 derniers jours)
BC
BC le 5 Mai 2021
Commenté : Adam Danz le 5 Mai 2021
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

Réponse acceptée

Adam Danz
Adam Danz le 5 Mai 2021
> 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 commentaires
BC
BC le 5 Mai 2021
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 le 5 Mai 2021
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])

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Visual Exploration dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by