File Exchange Pick of the Week

Advanced MATLAB: Capture mouse movement 33

Posted by Doug Hull,

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

iconPod.jpg

33 CommentsOldest to Newest

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

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

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

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.

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});

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?

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

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

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…

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

Hi Doug,

I just wanted to say thanks for this video. I was looking around on the web for a while about how to use mouse events in GUIs, but at first I only found complicated “tutorials.” Yours was very simple and basic, which is just what I needed!

I had the same problem Everest had with an older version of MATLAB not sharing handles across functions. However, I was able to work around it by assigning each handle a unique ‘Tag’, then using findobj(…) to lock to the appropriate object.

Thanks again,
-Matt-

Hi

I have a problem plotting to GUI axes in an outside function (in a different M file). This is what I have been trying to do.

function plotButton_Callback(hObject, eventdata, handles)
% hObject    handle to plotButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%get the current list of file names from the listbox

outsideFunction(handles);

and in another file outsideFunction.m

function outsideFunction(handles)

x = 1:0.1:10;
y = 3x^2 - 2x +5;

axes(handles.myAxes)
plot(x,y);

I get: Error using ==> axes
Invalid object handle

Hi Doug, This was a great demo. I’ve got the code implemented in my GUI perfectly. I’ve basically got a set of vertical lines that I’m using the mouse to drag up and down. If I grab one, I set it so they all move together. I’m wondering if you have a suggestion on adding a Ctrl+click and grab functionality that I could use to stretch or shrink all of the lines together. Thanks!

Thanks Doug. I’m also trying to make a text object draggable using the same principle. As far as I can tell, the code is correct but the callbacks when clicking on text objects are not being called. Is there a limitation with text objects that I’m not aware of?

I am trying to plot and use a line dragger to get the x value of the plot, but it’s not working. Please help me where am I doing wrong:

%plot the graph
[InputFrame, Fs, nbits] = wavread(‘data.wav’);
f = figure
plot(InputFrame);
%ylim([0,1])
%xlim([0.5,0.5])
subplot(3,1,2)
aH = axes(‘Xlim’,[0,1],’Ylim’,[0,1]);
%aH = axes(‘Xlim’,xlim,’Ylim’,ylim);
h = line([0.5,0.5],[0,1],…
‘color’,'red’, …
‘linewidth’,1, …
‘ButtonDownFcn’,@startDragFcn);
set(f,’WindowButtonUpFcn’,@stopDragFcn);
%get X data from the line
%a = get(h,’XData’);

function startDragFcn(varargin)
set(f,’WindowButtonMotionFcn’,@draggingFcn)
end

function draggingFcn(varargin)
pt = get(aH,’CurrentPoint’);
set(h,’XData’,pt(1)*[1,1]);
end

function stopDragFcn(varargin)
set(f,’WindowButtonMotionFcn’,”);
end
end

Hi I have a question here, I am trying to read data from a USB device (a hub for EMG sensors). Now I can’t understand the visa thing, can you explain it to me?

My mail is mom.sr@hotmail.com

Thank you,

PS: ur videos rock!

Great tutorial….but how would one know if the mouse is stationary? Right now, I am able to record mouse position when it moves, but I also want to record mouse position continuously though it is not moving…is there any way for me to do that? Thanks..

I have tried following the examples above but I’m still running into some trouble. When myfunction is called I get the following error:

“Error using projectbrowser>axes1_ButtonDownFcn (line 322)
Not enough input arguments.
Error while evaluating axes ButtonDownFcn
Undefined variable handles.”

function axes1_ButtonDownFcn(hObject, eventdata, handles)
set(gcf,’WindowButtonUpFcn’,{@mouseButtonMove,handles});

function mouseButtonMove(hObject, eventdata, handles)

handles

This was an awesome demonstration, thank you. New to matlab GUIs and callbacks, I used it to help me write an interactive quadrat selection tool.

How can I pick a point in the figure clicking with the mouse and store in an array?
Thanks

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