How do I extract data points from a plot?

2,931 views (last 30 days)
I would like to extract data points from a plot.

MathWorks Support Team on 17 Mar 2021
Edited: MathWorks Support Team on 17 Mar 2021
You can get the data from a plot by accessing the XData and YData properties from each Line object in the axes.
1. Make the figure containing the plot the current figure. An easy way to do this is to click the figure to bring it to the foreground.
2. Call the gca command to get the current axes within that figure. Then pass the axes to the findobj function to search for all lines in the axes. For example, here is a plot containing one line.
figure
plot([1 2])
ax = gca;
h = findobj(gca,'Type','line');
3. Get the coordinates from the XData and YData properties of the Line object.
x = h.XData;
y = h.YData;
If the plot has multiple lines, h is returned as an array of Line objects. Use array indexing to access each Line object in h. Then you can get the XData and YData properties from each Line object.
For example, here’s a plot containing three lines.
figure
plot([1 2 3; 4 5 6])
ax = gca;
h = findobj(gca,'Type','line');
x1 = h(1).XData;
y1 = h(2).YData;
x2 = h(2).XData;
y2 = h(1).YData;
x3 = h(3).XData;
y3 = h(3).YData;
Walter Roberson on 5 Apr 2021
Suppose you have two graphics objects h1 and h2, both of them with increasing X values, but the X values might not be exactly the same. For example, the second X series might be roughly 3 milliseconds after the first X series because of time spent acquiring the data.
In such a case, to align the data values:
x1 = h1.XData;
y1 = h1.YData;
x2 = h2.XData;
y2 = h2.YData;
y2aligned = interp1(x2, y2, x1, 'linear', 'extrap');
Now you have
x1 -- used for both sets of data
y1 -- y for the first set of data
y2aligned -- y for the second set of data, calculated at common time x1

Rakesh M on 4 Jun 2020
how to extract data if figure has two independent plots..the above code is extracting data from one plot of the figure

Jhonantans Rocha on 25 Aug 2014
Man, this really help me. Thanks!

MANIMARAN SELVAM on 2 Nov 2015
how to extract points from the output PV and IV graph
Walter Roberson on 2 Nov 2015

Stefanos Doltsinis on 16 Apr 2016
that code works nicely!
I just have one issue. I want to extract data from a subplot(2,1) and the extracted data are only from the second plot. How can I receive data from the first plot?
@MANIMARAN SELVAM I want to extract data from the same plot as you want (Photovoltaic array simulink module) and the code above works fine. At least for the Power against Voltage diagram. The data you need will be in x in the form of an array and will have the following sequence x{1} is the maximum voltage x{2} is the maximum power x{3} are all the data points on the graph.
I hope this helps.
Walter Roberson on 22 Apr 2016
In the above code, replace the line
h = findobj(gca,'Type','line')
with
h = findobj(subplot(2,1,1),'Type','line')

swagatika panigrahi on 13 Feb 2017
How to extract if the data points are defined using triangle or square symbol...I want to find the xdata and ydata at 'triangle and ''square' point on d figure
Akib Rahman on 25 Oct 2018
@Roberson your code works well. If you take a look at this questions I will be grateful https://www.mathworks.com/matlabcentral/answers/425878-how-do-i-extract-value-from-a-cell-array-to-text-file-or-csv-file .

Akshay Kumar on 8 Jul 2018
I am facing difficulties in accessing the plot data using the above method. The plot is generated using EEGLAB toolbox. The plot has 42 lines, however, after following the above mention step, the x_data and y_data do not contain the required data. Please advice.

Adarsh Vasa on 13 Apr 2020
Worked for me in R2019b. I'm new to coding so I had to manually extract data from the cell set of doubles. But that was a 10 minute work. I used it to extract data for 4 curves in my figure. Thanks a lot.