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.
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.
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’.
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:
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.
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.
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. See this video for more details:
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
Leave a Reply
About
Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.
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
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
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
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
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
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:
http://blogs.mathworks.com/pick/2007/12/26/advanced-matlab-buttondownfcn/
Hi Doug,
Nice video, One question though:
How does the “highlightPair” function knows the variables hdd & hdp?
Thanks,
Yonathan
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. See this video for more details:
http://blogs.mathworks.com/pick/2008/02/01/matlab-basics-nested-functions/
Thanks,
Doug
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
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