Asked by MathWorks Support Team
on 24 Jul 2013

I would like to extract data points from a plot.

Answer by MathWorks Support Team
on 27 Sep 2016

Accepted Answer

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)

Answer by Jhonantans Rocha
on 25 Aug 2014

Man, this really help me. Thanks!

Sign in to comment.

Answer by MANIMARAN SELVAM
on 2 Nov 2015

how to extract points from the output PV and IV graph

Walter Roberson
on 2 Nov 2015

Sign in to comment.

Answer by 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')

Sign in to comment.

Answer by swagatika panigrahi
on 13 Feb 2017

Walter Roberson
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
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
on 25 Oct 2018

Sign in to comment.

Answer by Akshay Kumar
on 8 Jul 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Matt (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/100921-how-do-i-extract-data-points-from-a-plot#comment_386712

## Natch Ruengsakulrach (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/100921-how-do-i-extract-data-points-from-a-plot#comment_394201

## Adusumilli Bala Surendra (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/100921-how-do-i-extract-data-points-from-a-plot#comment_632194

## ABHISHEK PARIDA (view profile)

Direct link to this comment:https://uk.mathworks.com/matlabcentral/answers/100921-how-do-i-extract-data-points-from-a-plot#comment_675543

Sign in to comment.