Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Doug’s Pick of the Week

January 28th, 2008

Practical Example: Adding callbacks to a plot

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.

iconFiles.jpgiconFree.jpgiconPod.jpglazy.jpg

10 Responses to “Practical Example: Adding callbacks to a plot”

  1. Dan Mac replied on :

    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

  2. Doug replied on :

    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

  3. Markus replied on :

    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

  4. Doug replied on :

    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

  5. Will replied on :

    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

  6. Doug replied on :

    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/

  7. Yonathan Nativ replied on :

    Hi Doug,

    Nice video, One question though:

    How does the “highlightPair” function knows the variables hdd & hdp?

    Thanks,

    Yonathan

  8. Doug replied on :

    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

  9. Yonathan Nativ replied on :

    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

  10. Doug replied on :

    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

Leave a Reply


Doug Hull is an Application Engineer at The MathWorks. A MATLAB user since 1994, he gets paid to live, eat, and breathe MATLAB! This blog is dedicated to promoting the File Exchange by highlighting files and original video content.



  • pierre: Hi sherryl and thank you for answering me, Actually, I already tried before to use this property because I...
  • Sherryl: In Response to Post #10 by Bryan - Hi Bryan, By default the analog input object will acquire one second...
  • Sherryl: Hello Pierre, Please look at the OutOfDataMode property. http://www.mathworks .com/access/helpd...
  • Scott Hirsch: Elya - In v7.0, try aviread. This has straightforward syntax for reading a single frame - you could...
  • Scott Hirsch: Eric - That’s a nice suggestion. I often get frustrated when debugging GUIDE guis and ending up...
  • pierre: Hi all, I have the Data Acquisition Toolbox, and I’m trying for 2 weeks to send a step voltage, and...
  • Eric S: It would be great to stop the debugger from coughing somewhere inside the more “internal̶ 1;...
  • Tareq: coef1 = rand(1,3)-0.5; coef2 = rand(1,3)-0.5; lex1=roots(polyder(c oef1)) lex2=roots(polyder(c oef2)) hold all...
  • Luca Balbi: While we’re at it… Checking for a number to be zero is tricky in itself. We’re better...
  • david: perhaps some error checking is in order. After all, it is possible that our randomly generated quadratic...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics