File Exchange 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

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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