How do I extract data points from a plot?

조회 수: 1,035 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2013년 7월 24일
댓글: Walter Roberson 2021년 4월 5일
I would like to extract data points from a plot.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 3월 17일
편집: MathWorks Support Team 2021년 3월 17일
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;
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 4월 5일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by