Beginner issue with GCO logic
Afficher commentaires plus anciens
Hello, I am a student new with Matlab and I am learning to work with mouse input.
My objective is to make it so that when the figure is clicked, a new shape is created,
however when an already existing shape is clicked, no new shape is created but instead the
existing one is deleted. I wrote the following code below and I am really unsure as to why the
else statement executes regardless. I can seemingly only delete the previously created shape instead of
any on the scene. I would really appreciate if you could point me in the right direction as I have
been stuck on this for a few days now.
Kindest regards!
Here is the code:
clear, clc, close all
fig = figure('color','k');
ax = axes('Position',[0 0 1 1])
hold on
axis([0 5 0 5],'off')
h = 0;
while true
waitforbuttonpress
if ~isempty(h) & gco == h
delete(h);
else
xy = get(gca,'CurrentPoint');
h = fill(xy(1,1) + rand(1,8)-0.5, xy(1,2) + rand(1,8)-0.5,
rand(1,3),'EdgeColor','w'); % The random colours
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!