How to get data from figure?

조회 수: 2 (최근 30일)
Deniz
Deniz 2013년 11월 10일
답변: Walter Roberson 2013년 11월 11일
Hi,
I have a curve figure. How can i get the y-data (vector) for given x=linspace(0,R,c) out of figure?
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2013년 11월 11일
xd = get(get(gca,'children'),'xdata'); % return the plot data
yd = get(get(gca,'children'),'ydata');
x = 1 : 0.1 : 3; %places to sample at
y = interp1(xd, yd, x); %interpolate from stored data at locations to be sampled

추가 답변 (2개)

dpb
dpb 2013년 11월 10일
x = get(get(gca,'children'),'xdata'); % return the plot data
y = get(get(gca,'children'),'ydata');
y=y(x(x==linspace(0,R,c)));
Trivial example from keyboard to illustrate...
>> plot(rand(1,3))
>> xdat=get(get(gca,'children'),'xdata');
>> ydat=get(get(gca,'children'),'ydata');
>> xvec=linspace(0,1,2);
>> y=ydat(ismember(xdat,xvec))
y =
0.8147
>>
You'll get some other value depending on state of rng at the time but the process should be clear to retrieve the data from the plot and find a location.
NB that if your x values are non-integer values you may need to make the comparison "fuzzy" rather than exact as is shown here.

Deniz
Deniz 2013년 11월 10일
this solution doesn't work for me.
plot(rand(1,3))
xdat=get(get(gca,'children'),'xdata')
ydat=get(get(gca,'children'),'ydata')
xvec=linspace(1,3,0.1)
y=ydat(ismember(xdat,xvec))
what i get:
xdat =
1 2 3
ydat =
0.0462 0.0971 0.8235
xvec =
Empty matrix: 1-by-0
y =
Empty matrix: 1-by-0
I need the y-vector for a x-vektor:xvec=linspace(1,3,0.1)
Any other ideas?
Thanks
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 11월 11일
The last argument to linspace() needs to be a count, not an increment. Use ":" if you want an increment
1 : 0.1 : 3

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by