3

I have a plot figure and I want to retreive x and y cordinates when we select particular data point using mouse from plot figure.

Any ideas?

Satya Vamsi
  • 217
  • 3
  • 6
  • 13

4 Answers4

7

Another option is to use the button down function:

function mouseExample()
    h = plot(rand(10,1), 'o-');
    set(h, 'ButtonDownFcn',@buttonDownCallback)

    function buttonDownCallback(o,e)
        p = get(gca,'CurrentPoint');
        p = p(1,1:2);
        title( sprintf('(%g,%g)',p) )
    end
end

Note this will not only work on "data-points", but rather on interpolated (x,y) positions of where you clicked on the lines. You could process the result by searching for the nearest actual point, and test if the click was within a reasonable radius to accept it.

Obviously it is a lot easier to just use the data cursor mode as other have noted...

screenshot

Amro
  • 123,847
  • 25
  • 243
  • 454
5

Even if you do not have a data button, you can activate the data cursor mode via the command datacursormode. If you want to store the data points rather than display them, you can use a modified update function (based on the example given in the matlab documentation):

function getDataFromFigure()
% Plots graph and sets up a custom data tip update function
fig = figure;
a = -16; t = 0:60;
plot(t,sin(a*t))

% variable to store data points
myData = [];

% enable data cursor mode
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myUpdateFcn)
set(dcm_obj, 'enable', 'on')

% do disable data cursor mode use
% set(dcm_obj, 'enable', 'off')


    function txt = myUpdateFcn(dummy, event_obj)
        % Customizes text of data tips

        % read out data point
        pos = get(event_obj,'Position');

        % store data point
        myData(end+1,:) = pos;

        % no data shown on figure
        txt = {''};

        % or
        % data also shown on figure:
        % txt = {['Time: ',num2str(pos(1))],...
        %         ['Amplitude: ',num2str(pos(2))]};
    end
end

~edit~
Make sure that the function myUpdateFcn is nested within the main function (note the two end at the bottom of my example) to make sure that myData is known within the function. If nesting is not possible, make myData a global variable instead.

H.Muster
  • 9,297
  • 1
  • 35
  • 46
  • @ H.Muster, Thanks for your answer, i tried it and i got an error on figure which says 'error in custom datatip string function'. Can you help me on this. – Satya Vamsi Jun 28 '12 at 15:03
  • The example works without problems for me, hence, I guess that I cannot help without seeing your actual code. Maybe you can add the relevant part of the code to your question? – H.Muster Jun 28 '12 at 15:17
  • I posted my code as an answer, i tried to add code in the comment but i had a problem with number of characters. – Satya Vamsi Jun 28 '12 at 15:31
  • 1
    Why don't you post it in your question? – H.Muster Jun 28 '12 at 15:37
  • @H.Muster using `setappdata` and `getappdata` is also possible. Better approach than global variables IMHO. – HebeleHododo Feb 28 '13 at 08:24
  • @HebeleHododo: yes, normally I try to avoid global variables, but in this case I had the impression that the OP needs a rather clear and easy code example. – H.Muster Feb 28 '13 at 08:28
2

you have a little button called data cursor on the top of your figure. Click on it, and then double click on your figure, you should get what you want.

someDude
  • 63
  • 1
  • 5
  • My plot is in a uitab and i cannot have data button. I want to get x,y values into variable rather than to see on figure. – Satya Vamsi Jun 27 '12 at 20:21
1

You could use the Plotly MATLAB API to get text on the hover for web-based graphs.

The data shows up when you roll your mouse over a point, or click and drag to zoom. Here's the online version of the graph below. The image below shows hover text; you can also click the "data and graph" link to access the data behind your graph in a grid.

Note: I'm on the Plotly team.

api_path = '/path/to/plotly';
addpath(api_path);
api_key = 'key';
username = 'username';
signin(username, api_key);

x=ones(3000,1); x(1:1000) = 1; x(1001:2000) = 2; x(2001:3000) = 3;
y=ones(3000,1); y(1:1000) = lognrnd(0,1,1000,1); y(1001:2000) = lognrnd(0,2,1000,1); y(2001:3000) = lognrnd(0,3,1000,1);
s=struct('type','box','jitter',0.5);
layout = struct('title', 'Fun with the Lognormal distribution','yaxis',struct('type','log'));

plotly(x,y, struct('style', s));
response = plotlylayout(layout);
url = response.url
filename = response.filename

MATLAB Hover

Mateo Sanchez
  • 1,604
  • 15
  • 20