How to use the button callback function to reference existing curve handles now that handles are objects rather than doubles

5 visualizaciones (últimos 30 días)
Hello, I have code that previously worked in MATLAB 2014a because handles were doubles, not objects. I am trying to use the same basic idea in my MATLAB 2016b code.
I plot a variety of curves on the same plot and save the handle for each in a cell array. So after all of the curves are plotted I have a cell array let's call it: "handles" which is a 800x1 cell array that contains 1x1 line handles. If you index into "handles" for example like this: index = handles{1} you would get the line properties for that particular line/curve. In addition to saving all of my handles for a particular line, I have also given each line a ButtonDownFcn callback. So my plotting command looks like this and is in a for loop: handles{i,1} = semilogy(FigHandle1,abs(array(:,1)),abs(array(:,2)),'ButtonDownFcn',@lineCallback);
The problem comes about in my linecallback function. It is programmed so that when I click on a line in the plot it enters the linecallback function and gets the handle of the curve I just clicked on using currhandle = src. Previously when handles were doubles, I could then search my "handles" cell array and find the corresponding handle and then I would know which curve I had chosen. However, with the new object oriented handles, it says that my currhandle is not equal to any of the handles in my "handles" cell array. When I break it down and only plot one line when I click on it my currhandle still does not match my handle for the plotted line even though they are the same line. The two handles (currhandle and the corresponding handle in my "handles" cell array for the same line) appear to be visually equal, but when I use == as suggested in the documentation I get 0. So I have essentially lost my handle to this line and I need to be able to select the line I want get its src using the buttondownfcn and locate that handle in my "handles" cell array. However, it now seems to create an all new handle instead of maintaining the one I defined to it. As I mentioned this worked brilliantly in 2014a when handles were doubles. Is this something that just can't be done now? It really seems like it should still work or MATLAB has eliminated alot of functionality by switching their handles to objects.
  1 comentario
Adam
Adam el 8 de Sept. de 2017
An equality test should work in this circumstance (certainly it does when I did a quick test) unless you have changed any property of the line between plotting it and clicking on it (because an equality test for an object will test all its properties whereas an equality test on the previous double handles was dumb and only tested the double itself, which is in some cases what you want, of course).
You could give each plot handle a unique 'Tag' then instead of testing full object equality just search for the tag that matches src.tag. Or you could just pass 'i' into your lineCallback so that it will know straight away which index it is. This is a bit brittle though as it will go out of sync if you happen to change anything in your array of line handles after plotting (e.g. delete one or plot one over the top of a previous index etc).
handles{i,1} = semilogy(FigHandle1,abs(array(:,1)),abs(array(:,2)),'ButtonDownFcn',@(src,evt) lineCallback( src,evt,i ) );

Iniciar sesión para comentar.

Respuestas (1)

Mark Schwab
Mark Schwab el 8 de Sept. de 2017
Editada: Mark Schwab el 8 de Sept. de 2017
Hi,
Have you tried using the "gco" function in your callback? gco returns the current object. Since lines are now stored as object, creating the following function should return the line object when clicking on a specific line:
function out = returnLine(~,~)
out = gco;
end
Then you would set your callback function to @returnLine.
Please refer to the following documentation for more information on the gco function: https://www.mathworks.com/help/matlab/ref/gco.html

Categorías

Más información sobre Graphics Object Programming 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