Stuart’s MATLAB Videos

Watch and Learn

Practical Example: Adding callbacks to a plot 14

Posted by Doug Hull,

This eleven minute video shows how you can make plots on two axes such that when one point of a pair is clicked, they both are highlighted. In this video you will see:
  • LINE used instead of PLOT so that each data point has its own handle
  • Debugging when a nested function is not closed properly
  • Debugging when inputs are unspecified to a callback
  • Handle graphics manipulation of marker size
  • Lining up code for better readability
  • Frequent testing of code to minimize errors when they do occur
Also note, I have made a direct link the the Lazy Web open challenges page. There is a new challenge up. Don’t forget there are t-shirts for answering the one minute survey below. iconFree.jpglazy.jpg

14 CommentsOldest to Newest

Dan Mac replied on : 1 of 14
I've got a question about the editor. About 10 1/2 minutes into the video, you edited two lines at the same time (changing "set(hdd(pairNum)" to "set(hdd(current)". I was curious if you really did change both lines at the same time, and if so, how did you do it. Thanks
Doug replied on : 2 of 14
Dan, Sorry no, that was just video editing. In an effort to keep the video shorter this time, I was cutting out parts where I was silently typing. I highlighted them blue in an effort to give an idea that I was 'faking it'. Sorry for the confusion! Doug
Markus replied on : 3 of 14
I am glad you are using a technique I also use for GUIs, namely saving data in the 'userdata' field of objects and/or the main figure. In my opinion this is much clearer then using handles to nested functions. You can even store more information in the userdata field using a struct. It just needs some discipline and little more lines of code for adding or updating data: str = get(gcf, 'userdata'); str.myfield = something; set(gcf, 'userdata', str); Markus
Doug replied on : 4 of 14
I am normally an advocate of SETAPPDATA, GETAPPDATA. However, for this specific case I was storing a very small amount of data and this was a very easy way of doing this. Thanks for adding to this with your technique. Doug
Will replied on : 5 of 14
Great Post, I always tried to do this but could never get it to work properly. Is there a similar way to do this with a 'line' instead of a 'point'. If I were to make a line with a ButtdownFnc is there a way I could get the (x,y) coordinates of the clicking? I see that I could potentially plot each of the points individually as you did above, but it seems like there might be a more elegant way to do this. Thanks
Doug replied on : 6 of 14
plot(rand(1,10)) k = waitforbuttonpress; point1 = get(gca,'CurrentPoint') This will get you the current point that you click on. An example like this was done here: https://blogs.mathworks.com/pick/2007/12/26/advanced-matlab-buttondownfcn/
Yonathan Nativ replied on : 7 of 14
Hi Doug, Nice video, One question though: How does the "highlightPair" function knows the variables hdd & hdp? Thanks, Yonathan
Doug replied on : 8 of 14
Yonathan, hdd and hdp were in scope in the highlightPair function because it is a nested function and has access to the variables of the outer function. Thanks, Doug
Yonathan Nativ replied on : 9 of 14
Hi Doug, In your demo the main function has ended. I thought when the function ends all its variables die. The nested function is called through a callback, why does the callback revive the main function? Thanks again, Yonathan
Doug replied on : 10 of 14
Yonathan, Two things are happening here: There is a nested function, and a function handle. The function handle (the @ symbol creates it) snapshots the workspace of the main function upon creation. This is how the variable stay "alive". The scoping into the nested function is as discussed in my video. Thanks for a great question! Doug
Vishrut replied on : 11 of 14
Hey Doug, Really helpful post. Still have a question...Is there any way to show the number of entry on the plot...say I am plotting two plots using three arrays and I am interested in knowing where my first point (of all tree arrays) is on each chart! Is there any way to do it within this procedure? Thanks, Vishrut
dhull replied on : 12 of 14
Vishrut, You could add an annotation or change the title in the HighlightPair function to do this. You can do whatever you want in these callbacks to enhance the visualization. -Doug
Amit replied on : 13 of 14
Thanks for sharing Doug, what do you recommend if the problem was to select multiple points with a drag-drop mouse operation?