Challenge: How to interact with MATLAB figure using mouse events?
10 views (last 30 days)
Show older comments
Hi MATLAB Lovers..
Suppose I have a 2D empty matrix of 50x50 as A=zeros(50);
I use Figure1=imagesc(A); to plot this matrix.
What I want is the following:
1. When a user click on any pixel on Figure1, it automatically changes its value form zero to one.
2. MATLAB automatically update the matrix A.
Whoever answer this, deserves to be called: MATLAB NERD.
Thank you so much.
0 Comments
Accepted Answer
Massimo Zanetti
on 8 Oct 2016
Also, try this simple version. You can update 5 pixels here, however choose the number in the for loop.
I=zeros(20,20);
h=image(I);
for k=1:5
p=ginput(1);
I(round(p(1,2)),round(p(1,1)))=100;
set(h,'CData',I);
drawnow;
end
3 Comments
Mohamed Nedal
on 6 Apr 2018
Hello, please if I want to do this for a FITS-image file, How can I do that? The FITS file is an array with No. of rows represents frequency and the No. of columns represents time and the value inside each cell represent the intensity of a signal at (f,t). I tried your code but there's no "CData" in the file.
Walter Roberson
on 7 Apr 2018
The I variable should be set to the data in the file, provided that it is real-valued. If it is not real valued then you may need to use the real() or the abs() values
More Answers (2)
Walter Roberson
on 8 Oct 2016
Create an image object using image() or imshow(). Set the ButtonDownFcn callback to a routine. In the callback, get() the axes CurrentPoint property . round() to get the coordinates. Flip the bit at that location. set() the CData property of the image object to the updated image.
1 Comment
Walter Roberson
on 9 Oct 2016
Get the axes CurrentPoint property, not the figure CurrentPoint
I=zeros(5);
x=1:5;
imagesc(x,x,I,'ButtonDownFcn',@lineCallback);
function lineCallback(ImageHandle, Structure1)
CursorLocation = get(ancestor(ImageHandle,'axes'), 'CurrentPoint');
disp(CursorLocation);
x = CursorLocation(1,1);
y = CursorLocation(1,2);
curCData = get(ImageHandle, 'CData'); %get the current version
col = round(x); %x is COLUMN!
row = round(y); %y is ROW!
col = min(max(1, col), size(curCData,2)); %in case it was out of range
row = min(max(1, row), size(curCData,1)); %in case it was out of range
curCData(row, col) = ~curCData(row, col); %flip the bit
set(ImageHandle, curCData); %send the change to the graphics
drawnow(); %render it
end
See Also
Categories
Find more on Graphics Performance 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!