필터 지우기
필터 지우기

Challenge: How to interact with MATLAB figure using mouse events?

조회 수: 10 (최근 30일)
Tamim
Tamim 2016년 10월 8일
댓글: Walter Roberson 2018년 4월 7일
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.

채택된 답변

Massimo Zanetti
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
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
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
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
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

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


Tamim
Tamim 2016년 10월 9일
Many thanks Walter for your answer. As I was not aware of it, last night I spent the time learning about ButtonDownFunc as a CallBackFun. It's absolutely a brilliant idea, and give more flexibility to execute more functions.
The problem I'm facing in this particular example is that, when I use "get(gcf,'CurrntPoint')" the returned numbers are out of range of the figure axis!! You said, I have to use "round() and then flip the bit! Could you please elaborate more on that?
Many thanks again for your response.
P.S.This is the code I used:
clc;
I=zeros(5);
x=1:5;
imagesc(x,x,I,'ButtonDownFcn',@lineCallback);
function lineCallback(Figure1,Structure1)
CursorLocation=get(gcf,'CurrentPoint');
disp(CursorLocation);
end
Regards,

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by