 ## How do I extract data points from a plot?

### MathWorks Support Team (view profile)

on 24 Jul 2013
Latest activity Commented on by John

### John (view profile)

on 27 Apr 2019
Accepted Answer by MathWorks Support Team

### MathWorks Support Team (view profile)

I would like to extract data points from a plot.

### MathWorks Support Team (view profile)

on 27 Sep 2016

In order to retrieve data values from a plot follow these steps:
1. Open the figure
2. In the MATLAB Command Window type the following commands:
h = findobj(gca,'Type','line')
x=get(h,'Xdata')
y=get(h,'Ydata')
Note that the dot notion feature is available starting from MATLAB 2014b, so instead of using the "get" function, you may do the following
x = h.Xdata;
y = h.Ydata;
In this example the plot was a plot of a line. The FINDOBJ function locates graphics objects with specific properties, for more information on this command type:
web([docroot,'/techdoc/ref/findobj.html'])
at the MATLAB Command Window.
If you have more than one line on the plot, the GET function will return a cell array. To retrieve the numbers inside of this cell array, use indexing. For example, to retrieve the points in the first line,
>> x{1}
>> y{1}
Or, if the lines have the same number of points, use the CELL2MAT function. Following the example above, to retrieve the numbers inside of cell array 'x' use:
X2=cell2mat(x)

### Adusumilli Bala Surendra (view profile)

on 3 Nov 2018
Thank you ver much. This is the easier way.
ABHISHEK PARIDA

### ABHISHEK PARIDA (view profile)

on 27 Feb 2019
Hello
I have a plot which has two lines each from different sensor. The plot has time in X-axis and sensor values in Y-axis. I have to find what is the value each sensor giving at a time i.e for a value on X-axis, I have to find corresponding Y-axis valus for the both the lines(or sensors).
This code gives data from both the lines but the correspon to different time(or X-axis) value.
John

### John (view profile)

on 27 Apr 2019
Thanks. I needed this.

### Jhonantans Rocha (view profile)

on 25 Aug 2014

Man, this really help me. Thanks!

### MANIMARAN SELVAM (view profile)

on 2 Nov 2015

how to extract points from the output PV and IV graph

Walter Roberson

on 2 Nov 2015

### Stefanos Doltsinis (view profile)

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

### Walter Roberson (view profile)

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 (view profile)

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

Walter Roberson

### Walter Roberson (view profile)

on 13 Feb 2017
Which plotting calls were used?
ax = gca;
obj_with_xd = findobj(ax, '-property', 'XData');
obj_with_right_markers = findobj(obj_with_xd, 'Marker', '^', '-or', 'Marker', 'v', '-or', 'Marker', 's');
the_xd_cell = get(obj_with_right_markers', 'XData');
if ~iscell(the_xd_cell); the_xd_cell = {the_xd_cell}; end
the_yd_cell = get(obj_with_right_markers, 'YData');
if ~iscell(the_yd_cell); the_yd_cell {the_yd_cell}; end
Now the_xd_cell and the_yd_cell are cell arrays containing the xdata and ydata of the objects that have markers v (downward triangle) or ^ (upward triangle) or s (square).
Akib Rahman

### Akib Rahman (view profile)

on 24 Oct 2018
Hello @Roberson! I have a figure of ECG signal. In this signal, I detect RQS point and after detecting this point I use the different sign for pointing this graph. Now I want to extract the value of this detected point in that way, such that: R1: X-level: 42, Y-level: 0.908 again R2: X-level: 137, Y-level: 0.854 and so on for all detected point of RQS (Q1, Q2...). How can I do that? I also attached my figure in this comment also. After finding this value I also want to save this value in a text file.
Help me. Akib Rahman

### Akib Rahman (view profile)

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 .