MATLAB Answers


get line plot data just by mouse stay on it (without click)

Asked by Morteza
on 27 Jan 2013

I plot my data and I need to show data (Y) in text box when I put the mouse cursor on it (without click), is there any fuction that help me?




No products are associated with this question.

3 Answers

Answer by Teja Muppirala
on 28 Jan 2013
 Accepted answer

Here is a simple example that outlines how to do it using HITTEST.

function my_example
function getcoords(varargin)
this = hittest;
if strcmp(get(this,'type'),'line');
    cp = get(gca,'currentpoint');
    ht = title(num2str(cp([1 3])));


Answer by Walter Roberson
on 28 Jan 2013

datacursormode() with a custom callback function that sends the coordinate to the edit box as well as displaying it on the screen.

  1 Comment

on 28 Jan 2013

I knew that datacursormode can return current position mose on the figure, but it is require to call the function and the click on the screen to get result. In my plot just by move the mouse program should realize that current mouse position is on the line plot if you are already. I found a function like overobj2.m and put it in the function that is called by my program every secound to get data, but it just return only axes .....

Answer by Jan Simon
on 28 Jan 2013

You can use the WindowsButtonMotion function of the parent figure. There OVEROBJ can check, if the mouse is over the specified axes and the CurrentPoint property of this axes contains the coordinates. Now you need a method to find the nearest (interpolated?) point of your data for the display.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!