File Exchange Pick of the Week

May 27th, 2008

Advanced MATLAB: Capture mouse movement

This week we will take a look at how you can capture the mouse movement through callbacks. As the mouse moves, we will update the xdata of a line so that you are moving the line with the mouse.

Video Content

iconFiles.jpgiconPod.jpg

14 Responses to “Advanced MATLAB: Capture mouse movement”

  1. vincent replied on :

    hee Doug,

    this is a really nice feature… I want to use it for my GUI as well, but if I apply it on real data (obtained from a racecar) then Matlab gives the following error:

    Warning: line XData length (2) and YData length (500) must be equal.

    and all I did with your m-file was replacing the definition of h into:

    h=plot(data(1:500,1),data(1:500,4),’ButtonDownFcn’, @startDragFcn)

    and the plot disappears from the figure.
    how can I apply this feature to a dataplot?

    cheers Vincent

  2. Doug replied on :

    Vincent,

    Assign these variables as a trouble shooting step:

    x = data(1:500,1)
    y = data(1:500,4)

    Check to see if these are the same size.

    Then try:

    plot(x,y) %skip button down for now

    Test that

    Finally go to this command

    plot(x,y,’ButtonDownFcn’, @startDragFcn)

    If you take these systematic steps, I think you will find the problem.

    Doug

  3. vincent replied on :

    yeah I solved that problem, the length of x was not correct. I have it working on my own data, very cool. now i want to put it in my gui, but matlab gave me the following error;

    ??? Input argument “handles” is undefined.

    Error in ==> Data_Postprocessor_v7>startDragFcn at 658
    Data=getappdata(handles.figure1,’mydata’)

    ??? Error while evaluating line ButtonDownFcn

    I use the above code at the start in every callback function of the gui to get the data. so it works in all the callback functions but when I put it in the ButtonDown function Matlab produces the above error. is it maybe because a callback function is not the same as the ButtonDown function?

    thanks Doug

    Vincent

  4. Doug replied on :

    Vincent,

    I would need to see the code to figure this out. Send it to support and we can figure it out.

    www.mathworks.com/support

  5. Jan Lauber replied on :

    Very cool. Works great as an intereactive thresholding

  6. Tahim replied on :

    I have a similar problem as Vincent. I tried to use these functions inside my GUI, although I think these created functions can’t see the handles of the GUI.
    My startDragFcn is written basically the same way as Vincent:

    startDragFcn(hObject, eventdata, handles)
    %Inside this function handles is not availabel and I need the axes that is stored in the handles.

    When I click over the line I get ??? Input argument “handles” is undefined.

  7. Tahim replied on :

    I was looking for an answer for this problem in matlab web page and I found the solution. When using the functions presented by the Doug (nice work Doug) all you have to do to use them inside your GUIs is to pass the handles of the GUI inside a cell.

    When using additional arguments for the callback function, you must set the value of the property to a cell array (i.e., enclose the function handle and arguments in curly braces). For example,
    You can define the callback function to accept additional input arguments by adding them to the function definition. For example,

    function startDragFcn(src,eventdata,arg1)

    arg1 is an additional argument. In my case I used the handles of the GUI as an additional argument:

    function startDragFcn(hObject,eventdata,handles)

    So, for the callback function you have to enclose the function handle and the additional arguments in curly braces. For example,

    set(hLine,’ButtonDownFcn’,{@startDragFcn,handles});
    set(hFigure, ‘WindowButtonUpFcn’,{@stopDragFcn,handles});

  8. Everest replied on :

    Hi!
    This is really great!:] I’ve used this idea to move the data points on my plot. But here is my query:
    the code works really great on Matlab 7.1, but when I run it on the older version (actually it is Matlab 6.1.0.450)
    I get the following error:

    ********************************************************
    ??? Undefined function or variable ‘f’.

    Error in ==> G:\MATLAB_61\MOUSE_CAPTURE\mouse_capt.m (startDragFcn)
    On line 66 ==> set(f,’WindowButtonMotionFcn’,@draggingFcn)

    ??? Error while evaluating line ButtonDownFcn.

    ??? Undefined function or variable ‘f’.

    Error in ==> G:\MATLAB_61\MOUSE_CAPTURE\mouse_capt.m (stopDragFcn)
    On line 128 ==> set(f, ‘WindowButtonMotionFcn’,”)

    ??? Error while evaluating figure WindowButtonUpFcn.
    ********************************************************

    Why is that? What to do to make it work on the older version?

  9. Doug replied on :

    Everest,

    I just did a quick check. That version of MATLAB is about three years ago. There have been enough changes in function handles that I suspect that is the reason code written for the newer version of MATLAB does not work in an older version.

    Doug

  10. Everest replied on :

    Thanks Doug!

    The problem is solved now.. Actually I’ve used UserData property to gave the name to my ‘figure’,axis’ and ‘line’ so I could call them in the external function (draggingFcn etc.)
    However, it was not so obvious at the beginning.

    Everest

  11. Aswath replied on :

    Hi!
    I’ve not been able to get the functions to work in a GUI.
    I am getting errors if I paste the code into my code, since other GUI functions dont have an ‘end’ statement.
    I would also like to know how to pass handles of objects inside the GUI to functions outside…

  12. Doug replied on :

    @Aswath,

    If one subfunction has an end statement, then they all must have one. That will help with the code pasting problem.

    As for handles structure:

    http://blogs.mathworks.com/videos/2008/04/17/advanced-matlab-handles-and-other-inputs-to-guide-callbacks/

    -Doug

  13. Lucy replied on :

    Dear Doug,

    I’m a Matlab newbie and I think your videos are fantastic – without them I would be lost!

    A question: I have created a simple GUI where data can be plotted and where coordinates can be obtained by pointing and clicking the mouse on the figure. However, I’m looking to obtain the coordinates from a figure in a more dynamic way. What I would like to be able to do is create a moving point which is fixed to the trajectory of the plot, which I drag up and down the line and obtain the plot coordinates - Yahoo interactive finance graphs have something similar. Is this possible in Matlab, and if so, could you give me any hints on what I would need to do to achieve this?

    Any suggestions would be very much appreciated!!

    Lucy

  14. Doug replied on :

    @Lucy

    I think this is what you seek to move a line with the mouse in MATLAB.

    http://blogs.mathworks.com/videos/2008/05/27/advanced-matlab-capture-mouse-movement/

    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.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

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