How can I access image from image's button-down event handler?
-
14-06-2021 - |
Question
I have following code in initialization
im = imread('Image02.tif');
figure(); imagesc(im); colormap(gray);
[hImage hfig ha] = imhandles(gcf);
set(hImage,'ButtonDownFcn',@clickInImage);
And clickInImage function looks like this
function clickInImage(s,e)
pt=get(gca,'Currentpoint');
x=pt(1,1);
y=pt(1,2);
...
My question: How can I access image im
in clickInImage
function? I can't use global variable.
Solution
You could retrieve the image inside the callback using:
img = get(s, 'CData');
Otherwise, make the callback a nested function inside your main GUI function, that way you get access to all its parent workspace:
function myGUI()
img = imread('coins.png');
figure
hImg = imagesc(img); colormap(gray)
set(hImg,'ButtonDownFcn',@clickInImage);
function clickInImage(src,evt)
%# here you can access `img` directly ...
img;
end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow