Use ButtonDownFcn on an image

I have a (pixelated) image that has been created from an array of data using the function imagesc(). I would like to click on a pixel and be able to a)obtain the coordinates of the pixel in the figure window, and b)pass these values to a piece of code (to be created) which "finds" what the data values at those coordinates were, and displays the data values in a new figure window. Is such an extraction method, where I am not,say,clicking on a line or axis of a bar graph, possible using the ButtonDownFcn?

댓글 수: 5

If you add the ButtonDownFcn to your axes instead then you can use the
'CurrentPoint'
property of the axes to give you the location. To do this though you must set the
'HitTest'
property of your image to 'off', otherwise the image will pick up the mouse click instead of the axes.
J.S.
J.S. 2018년 7월 10일
Thank you, Adam. I assume that I create my initial figure in my desired script, and that the piece which obtains the location clicked is included at the bottom of this same script? MATLAB will not throw an error because I don't click on the image instantaneously after the image is created?
J.S.
J.S. 2018년 7월 10일
Also, (sorry, I just thought of this) is it possible to click multiple times on the image (in different places), and each time view the corresponding data? If so, can that be done via a single ButtonDownFcn call (no for loops)? Thanks for your help!
Adam
Adam 2018년 7월 11일
I would use functions rather than scripts, but scripts do at least allow nested functions now so you can create your callback as a function at the bottom of your script, to do whatever you want it to with the 'CurrentPoint'.
This function will be called every time you click on whatever you have attached it to, so if this is the axes (assuming you have turned the hittest of the image off) then each time you click the axes the function will trigger so no loops should be needed.
J.S.
J.S. 2018년 7월 11일
Thanks!

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

릴리스

R2015b

질문:

2018년 7월 9일

댓글:

2018년 7월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by