In Matlab, I would like a context menu to open when I right-click on a data point in a plot. The selected action should call a function where I would like to access the coordinates of the data point for further processing.
The Matlab example is:
cm = uicontextmenu(fig);
m = uimenu(cm);
m.Text = "Function description";
m.MenuSelectedFcn = @(src,event)myFunction(src,event);
function myFunction(src,event)
...
end
plot(x, y, "ContextMenu", cm);
This works as expected in that myFunction is called when I right click on a data point and click on the action in the context menu. However, I cannot access the x,y values of the data point within myFunction.
If you really want to implement a custom function then you can use the
CurrentPointproperty of the current axes, so your function would be something likeIn use:
However, the functionality you're describing can be achieved natively (without a custom callback) using the datatip cursor and the
Export Cursor Data To Workspace...option.Other advantages include
You have even more options using the in-built brushing tool, where you can brush areas of your data and then