Challenge: How to interact with MATLAB figure using mouse events?
조회 수: 10 (최근 30일)
이전 댓글 표시
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
채택된 답변
Massimo Zanetti
2016년 10월 8일
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
Mohamed Nedal
2018년 4월 6일
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
2018년 4월 7일
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
추가 답변 (2개)
Walter Roberson
2016년 10월 8일
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
Walter Roberson
2016년 10월 9일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!