Challenge: How to interact with MATLAB figure using mouse events?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tamim
le 8 Oct 2016
Commenté : Walter Roberson
le 7 Avr 2018
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 commentaires
Réponse acceptée
Massimo Zanetti
le 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 commentaires
Mohamed Nedal
le 6 Avr 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
le 7 Avr 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
Plus de réponses (2)
Walter Roberson
le 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 commentaire
Walter Roberson
le 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
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!