Get position when using uicontextmenu
Show older comments
I'm trying to code an interface that alows the user to perform some actions on a set of points visualized as a scatter plot.
To do so, I thought I could use a contextmenu, where the user could right-click on a point and decide what to do among a list of actions.
However when using uicontextmenu, I could not get nor the handle of the parent, nor the position (or index) of the point the user is clicking.
Consider this example
function test()
figure
x = randn(1,20);
y = randn(1,20);
menu = uicontextmenu();
hs = scatter(x,y,'UIContextMenu',menu);
uimenu(menu,'Label','Do something with this point','Callback',@something)
end
function something(src,evnt)
fprintf('x = %f, y = %f',x,y)
end
I know a workaround is to pass the parent to the function, is to call
uimenu(menu,'Label','Do something with this point','Callback',{@something, hs})
and define
function something(src,evnt,parent)
fprintf('%s\n',class(parent))
end
but this doesn't help me understanding on which point the user has clicked, as the input to "something" is defined during creation ans src,evnt do not contain the information I'm looking for.
Is there a workaround?
Accepted Answer
More Answers (1)
Adam
on 15 Oct 2019
I use code like this to position my context menu under the cursor on an axes, though if you already have the figure handle at the ready all you need is the second line to give you your current location. In my case I just set this value as the 'Position' property of the context menu, but you can use it for whatever purpose you wish.
hFig = ancestor( hAxes, 'figure' );
hFig.CurrentPoint;
This will give the figure position, but the axes also has a 'CurrentPoint' property which I would assume will give you the current point in axes coordinates. I can't remember off the top of my head as my example does not do this and uses the figure CurrentPoint instead.
Categories
Find more on Interactive Control and Callbacks 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!