Borrar filtros
Borrar filtros

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

10 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Massimo Zanetti
Massimo Zanetti el 8 de Oct. de 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 comentarios
Mohamed Nedal
Mohamed Nedal el 6 de Abr. de 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
Walter Roberson el 7 de Abr. de 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

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 8 de Oct. de 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 comentario
Walter Roberson
Walter Roberson el 9 de Oct. de 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

Iniciar sesión para comentar.


Tamim
Tamim el 9 de Oct. de 2016
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,

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by