How to extract information from a figure callback functions?

4 views (last 30 days)
I want to perform actions when the mouse is used inside a figure. I already figured out how to do that thanks to the example in the Matlab help 'Coding Window Button Callback Functions'. What I cannot do is to extract variables values from inside the callback functions - for example this code (which was written by Matt Tearle in an answer to a question):
figure
plot(rand(5))
set(gca,'buttondownfcn',@mybttnfcn)
function mybttnfcn(h,~)
hf = get(h,'parent');
b = get(hf,'selectiontype');
xy = get(gca,'CurrentPoint');
if strcmpi(b,'normal')
text(xy(1,1),xy(1,2),'Left click')
elseif strcmpi(b,'alt')
text(xy(1,1),xy(1,2),'Right click')
else
text(xy(1,1),xy(1,2),'Careful there, crazy man!')
end
Suppose I want to get the value of 'xy' outside of the function 'mybttnfcn', how do I do that? I tried some ideas but without success.
Thank you.

Answers (0)

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!