I'd like to plot some points over an image in a dialog.
Show older comments
I want to be able to select one point with a left-click, a second point with a right-click, but I'm running into all sorts of trouble.
This dialog calls nested function replot() on creation, and in the ButtonDownFcn() callback.
In this form, the image never shows up but the points do. If I un-comment the colormap line, everything looks right when the dialog is opened, but the image disappears again when the callback triggers.
To boot, I can't debug effectively because breakpoints inside replot() don't work.
The heck is going on? Alternatively, is there a better, lightweight way to accomplish this?
im = rgb2gray(imread('peppers.png'));
stats(1).Centroid = [260 260];
stats(2).Centroid = [350 260];
dHandle = cDialog(im,stats);
function d = cDialog(im, stats, d)
ddims = size(im).*1.5;
if nargin < 3
dp = [50 50 51+fliplr(ddims)];
else
dp = d.Position;
end
d = dialog('Units','pixels','Position', dp, ...
'Name','Confirm Selections');
ax = uiaxes('Parent',d,...
'Units','pixels',...
'Position',[25 10 dp([3,4])-25],...
'Visible','off');
ax.Title.String = 'Confirm or Select points (Left- & Right-click)';
% Show the current points
replot;
uiwait(d);
%%% Nested functions
function replot
hold(ax,"off")
image(im,'Parent',ax,'ButtonDownFcn',@clicked);
drawnow
hold(ax,"on")
% colormap(ax,parula);
pts = {stats(1).Centroid;stats(2).Centroid};
if ~isempty(pts{1})
scatter(ax,pts{1}(1),pts{1}(2),'bo','filled')
end
if ~isempty(pts{2})
scatter(ax,pts{2}(1),pts{2}(2),'ro','filled')
end
end
function clicked(~,event)
switch event.Button
case 1
stats(1).Centroid = event.IntersectionPoint([1 2]);
case 3
stats(2).Centroid = event.IntersectionPoint([1 2]);
otherwise
end
replot;
end
end
3 Comments
Chris
on 27 Jan 2023
Jan
on 28 Jan 2023
@Chris: Please do not use notifications to attract specific users. I've read your question before and do not understand, what you want to achieve and have no idea, what I could answer. Now I got your notification, read the question again, found out, that it does not contain new information and still cannot help you. So this was a waste of time.
Imagine what will happen, if all users send notification to some preferred members: They will drown in a pile of messages and this reduces their time to post answers in the forum. So please don't do this. Thank you.
Today I got 4 of such notifications. I've disabled the possibility to contact me over my profile page already for these reasons.
Accepted Answer
More Answers (0)
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!