Retrieving X and Y values from Matlab Plot

有些话、适合烂在心里 提交于 2019-11-28 00:29:37

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...

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!