File Exchange Pick of the Week

December 26th, 2007

Advanced MATLAB: ButtonDownFcn

This is the next in a series of advanced topics in MATLAB. My definition of “Advanced” is somewhat nebulous and arbitrary, so even newer users can give this a watch.

Two and a half minute video shows how to use buttondownfcn to get the current point. This can be a fundamental skill that underlies many advanced GUIs.

Video Content

Find the files here.

Other videos have been gathered here:
http://blogs.mathworks.com/pick/category/video/

Other Advanced MATLAB posts have been gathered here:
http://blogs.mathworks.com/pick/category/advanced-matlab/

11 Responses to “Advanced MATLAB: ButtonDownFcn”

  1. Daphne replied on :

    Thanks for another great tutorial.

    If my plot were actually a 2-D figure, would it be possible to get the (x,y) and also the intensity at that spot (x/255)?

    Thanks,
    Daphne

  2. CW replied on :

    I am having problem replicating the great example you have provided here. Specifically, I have create the following in an m file in an attempt to replicate what you have here…

    function[] = graphinteract

    figure(1)
    plot(peaks)
    set(gca,’buttondwnfcn’, @clicker)

    function clicker(gcbo,eventdata,handles)
    disp(get(gca,’Currentpoint’))

    However, I always get the following error…

    ??? There is no ‘buttondwnfcn’ property in the ‘axes’ class.

    Error in ==> graphinteract at 5
    set(gca,’buttondwnfcn’, @clicker)

    Any idea what I am doing wrong?

  3. Doug replied on :

    CW,

    ‘buttonDownFcn’ is spelled wrong in the above code.

    Doug

  4. Zach replied on :

    How can this function/code be used to modify the data by dragging it?

    If it cant be used to do this, then what function allows me to drag data points from a plot and modify the graph (and the new data) on the fly.

    Thanks

    Zach

  5. Doug replied on :

    Zach,

    I have done this kind of thing before, it does require some advanced maneuvers. Mostly it is about using ASSIGNIN to redefine a variable in the workspace as you modify it graphically. You need to place that function call in the end of the callback that modifies the data.

    -Doug

  6. Lorraine replied on :

    Doug,

    I try to use ButtonDownFcn to get information about image pixels in the current figure. The function works well before the image showed in axes, but after the image loading into the axes, the ButtonDownFcn does not work.

    here is the code

    %%%%%
    function pushbutton_input_vimage_Callback(hObject, eventdata, handles)

    [vname,vpath]=uigetfile(’*.bmp; *.img; *.jpg; *.tif’);
    vimage=[vpath vname];
    handles.user.plot_vimage=imread(vimage);

    axes(handles.Visible_iamge)

    image(handles.user.plot_vimage)

    guidata(hObject, handles);

    %%%%%
    function popupmenu_vpoints_Callback(hObject, eventdata, handles)

    if get(hObject, ‘value’)==2
    handles.mouse_track=2;

    elseif get(hObject, ‘value’)==3
    handles.mouse_track=3;

    elseif get(hObject, ‘value’)==4
    handles.mouse_track=4;

    end

    guidata(hObject, handles);

    %%%%
    function Visible_iamge_ButtonDownFcn(hObject, eventdata, handles)
    handles.mouse_pos_fig=get(handles.figure1,’currentpoint’);
    axes_area=get(handles.Visible_iamge,’position’);

    if (handles.mouse_pos_fig(1)(axes_area(1))) && …
    (handles.mouse_pos_fig(2)(axes_area(2)))
    handles.mouse_pos=get(hObject,’currentpoint’);
    else
    handles.mouse_pos=-1;
    end

    b=handles.mouse_pos(1,1:2);

    if handles.mouse_track==2
    set(handles.text_A,’string’,int2str(b));
    elseif handles.mouse_track==3
    set(handles.text_B,’string’,int2str(b));
    elseif handles.mouse_track==4
    set(handles.text_C,’string’,int2str(b));
    end

    guidata(hObject,handles);

  7. Doug replied on :

    Lorraine,

    Some of the graphics functions create a new axes and replace the old one. In doing this, the old callbacks, and certain other attributes are removed. You need to set them again.

    Doug

  8. Lorraine replied on :

    Doug,

    Thanks, but how can i do that?
    I tried to get the new axes by

    h = findobj(handles.Visible_iamge,…),but it returned nothing

    Lorraine

  9. Doug replied on :

    Lorraine,

    When you call the image command, capture the handle to that, then get the parent.

    Doug

  10. Lorraine replied on :

    Doug,

    It works now, thanks : )!

    Lorraine

  11. Steve L replied on :

    Lorraine,

    As Doug said in comment 7, by default some of the high-level graphics functions (including image) clear many of the properties of the axes. You can restore those properties after calling IMAGE (as Doug suggested) or you can SET the axes NextPlot property to ‘replacechildren’ before calling IMAGE.

    http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axes_props.html#NextPlot

    A similar type of problem, but with PLOT and the Tag property instead of IMAGE and the ButtonDownFcn property, is described in this document on our support website; the solution is the same.

    http://www.mathworks.com/support/solutions/data/1-168SX.html

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.