¿Cómo seleccionar un niño de ejes en matlab?
-
14-11-2019 - |
Pregunta
He estado buscando en todas partes, pero no puedo encontrar un sitio que muestre cómo hacer esto. Lo que quiero es poder seleccionar un objeto de un eje cuando lo haga clic, para que pueda cambiar sus colores y cosas.
No puedo descubrir cómo seleccionar un niño, puedo crear objetos pero no seleccionarlos.
Tengo esta pieza de código que utilizo para crear una línea:
coord = ginput (2)
x = coord(:,1)
y = coord(:,2)
hline = line(x,y)
No estoy seguro de si necesito crear los objetos en una matriz para que pueda seleccionar Editarlos / eliminarlos. Creo que necesitaría usar ButtondownFCN, pero probablemente estoy haciendo algo completamente equivocado.
Se apreciaría cualquier ayuda, si me falta alguna información, por favor, hágamelo saber
gracias
Solución
It is not necessary to use ginput
and extract the coordinates. This is done automatically by an built-in "listener" in the figure-window. You are correct in assuming that you can use the ButtonDownFcn
property on the object (line, lineseries, or other handle graphics object).
Try to create at simple line from (0,0) to (1,1):
hline = line([0,1],[0,1]) %# create line, save handle in hline
Then you can set the ButtonDownFcn
to, for instance, a function handle to an anonymous function:
set( ...
hline, ...
'ButtonDownFcn', @(handle,event)(disp(['You clicked on the line!'])) ...
);
Now try to click on the line. It should print the text in the command window.
The function needs to be able to receive atleast two arguments: (1) the handle of the object itself (the line) and (2) an "event structure". I believe the second argument is just empty when you use line-objects. But your function still needs to receive atleast these two arguments (even if you do not use them).
Read more here: http://www.mathworks.com/help/techdoc/ref/line_props.html.
You can also use your own function (a named function in a file):
set( ...
hline, ...
'ButtonDownFcn', @(handle,event)(namedFunction(handle,event)) ...
);
... or you can use a struct-array if you (expectedly) have other arguments beyound those two.