How can I access image from image's button-down event handler?

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.

Answers


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

Need Your Help

Removing dynamic controls : Clear is working but not remove

c# asp.net postback dynamic-controls createchildcontrols

I faced a recent problem, where I was generating the dynamic control on the selection of drop down. When the selection changes, I have to generate another set of dynamic controls, removing the exis...