getting data from plot ?
437 views (last 30 days)
Show older comments
lclk psdk
on 10 Sep 2013
Commented: Image Analyst
on 21 Apr 2023
I plot an x , y graph can i ask the programme to tell me the value of y for the value of x i want ? how i can write these at the command window ?
1 Comment
Accepted Answer
Image Analyst
on 10 Sep 2013
I agree with Jan - it's ambiguous. You already have x, and y since you plotted it, so there's no need to extract anything from the axes (graph) at all. So in that case, I'd just use the x and y which are already available, and if the x is in the x array that you plotted, you can do this:
index = find(x == desiredXValue); % May be multiple indexes, possibly
yDesired = y(index);
Now, if the desired x is not in your x array, then you can use interp1() to get the interpolated/estimated y value for that x.
yDesired = interp1(x,y, desiredXValue);
11 Comments
Image Analyst
on 21 Apr 2023
change is your y so
y = ((Temperature-Ti)/(Tf-Ti))*100
and x is time according to your plot(time, change) call. By the way, don't use time, or any other built-in function name as your variable name because it could lead to confusion and errors. And don't call one variable Time and another time - that's just downright confusing! But anyway, I can't figure out how your y depends on x. Can you give me an equation where y is a function of x? If you want Temperature as a function of change, it's simply
Temperature = (change/100) * (Tf - Ti) + Ti
More Answers (3)
Jan
on 10 Sep 2013
Edited: Jan
on 10 Sep 2013
The question is not clear. If you plot x versus y, the values are known and therefore the problem has not relation to the plotting. But perhaps you have a Figure file and lost the x and y values. Please explain this by editing the original questions.
Do you want the y values exactly at the position of an x value, or are you interested in x values between two given x values also? In the first case consider, that numerical comparisons are bounded by the numerically limited precision:
x = 0:0.1:1;
find(x == 0.3) % does not find a matching element!
So perhaps this helps:
axes;
x = 0:0.1:pi;
y = sin(x);
plot(x, y);
clear('x', 'y'); % for demonstration only
LineH = get(gca, 'Children');
x = get(LineH, 'XData');
y = get(LineH, 'YData');
xx = 0.724 % Any value
yy = interp1(x, y, xx) % Linear interpolation [EDITED, not "linspace"]
xx2 = x(5); % One exact value
index = find(x == xx2); % Of course this is 5
disp(y(index))
5 Comments
Image Analyst
on 15 Sep 2013
He's getting the data from the graph itself. It's as if you lost your original data, for example by calling the clear('x') command. If you did that unwise thing, then it's possible to extract your data from the graph, which has, in effect "stored" it.
Ilham Hardy
on 10 Sep 2013
h = gcf; %handle of current fig
axObj = get(h, 'Children');
datObj = get(axObj, 'Children');
xdata = get(datObj, 'XData');
ydata = get(datObj, 'YData');
Asimananda Khandual
on 27 Jan 2018
Respected Image Analyst gave the best answer; I am just simplifying it with examples.
>> x=1:1:100; >> y=5*x+10; >> yDesired = interp1(x,y, 10)
yDesired =
60
>> xDesired = interp1(y,x, 60)
xDesired =
10
=====================================================================================
>> y=5*x.^2+2*x+10; >> plot(y,x) >> xDesired = interp1(y,x, 60)
xDesired =
2.9630
>> yDesired = interp1(x,y, 10)
yDesired =
530
-----------------HELPFILES---------------
Vq = interp1(X,V,Xq,METHOD) specifies alternate methods.
The default is linear interpolation. Use an empty matrix [] to specify
the default. Available methods are:
'nearest' - nearest neighbor interpolation
'linear' - linear interpolation
'spline' - piecewise cubic spline interpolation (SPLINE)
'pchip' - shape-preserving piecewise cubic interpolation
'cubic' - same as 'pchip'
'v5cubic' - the cubic interpolation from MATLAB 5, which does not
extrapolate and uses 'spline' if X is not equally
spaced.
Vq = interp1(X,V,Xq,METHOD,'extrap') uses the interpolation algorithm
specified by METHOD to perform extrapolation for elements of Xq outside
the interval spanned by X.
Vq = interp1(X,V,Xq,METHOD,EXTRAPVAL) replaces the values outside of the
interval spanned by X with EXTRAPVAL. NaN and 0 are often used for
EXTRAPVAL. The default extrapolation behavior with four input arguments
is 'extrap' for 'spline' and 'pchip' and EXTRAPVAL = NaN (NaN +NaNi for
complex values) for the other methods.
PP = interp1(X,V,METHOD,'pp') will use the interpolation algorithm specified
by METHOD to generate the ppform (piecewise polynomial form) of V. The
method may be any of the above METHOD except for 'v5cubic'. PP may then
be evaluated via PPVAL. PPVAL(PP,Xq) is the same as
interp1(X,V,Xq,METHOD,'extrap').
0 Comments
See Also
Categories
Find more on Splines in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!