HOW extract a particular value from graph on X-axis using y = get(d,'Ydata')?

29 views (last 30 days)
I have a plot using lsline (least square fit line). I want to extract the 5000th value (coordinate) and store it to use it for the next iteration. using y = get(d,'Ydata'), from the plot,i get a range of 0-6000 and despite setting the axis range, i stil get the last value of 0=0.5364 and 6000=0.4734 respectively. I need value at 5000. How can i get the y-value @ 5000th but not 6000th value (which seems default).
I want to get the value of point marked 'X' on the image
im using the following codes to plot..
figure(1)
x=[kp2]; % @ X=5000; Y=kp2 gain
plot(x,'.')
d=lsline; % Using least square line method on scattered output
set(d,'color','r')
ylabel('kp2(gain)');xlabel('Time');title('kp2 Least Square')
grid on
%axis([0 5000 0 5])
y = get(d,'Ydata') % GET VALUE AT point 5000
Please help me.. i just want to obtain data value at a particular coordinate.
Thank you in advance!!

Accepted Answer

Star Strider
Star Strider on 3 Sep 2015
Using lsline to do a simple linear fit is taking the long way round. Use the backslant (\) operator with your known (x,y) values to estimate the parameters of the line and then use those parameters to calculate the desired estimated ‘y’-values from the given ‘x’-values:
x = [0 6E+3];
y = [0.5364 0.4734];
b = [[1; 1] x']\y'; % Linear Least-Squares Parameter Estimation
y5k = [1 5E3]*b; % Calculate ‘y’ At x=5000
figure(1)
scatter(x, y) % Plot Data
d = lsline;
hold on
plot(5E+3, y5k, 'rx', 'MarkerSize',10) % Plot ‘y’ At x=5000
hold off

More Answers (1)

Image Analyst
Image Analyst on 3 Sep 2015
Edited: Image Analyst on 3 Sep 2015
Since you're setting x equal to kp2 and then plotting x, what you're really doing is using the array index as the "x" value and the badly-named "x" as the "y" value. So, to get the "y" value (which is really x, which is really kp2) at an "x" (index) value of 500, you simply do this:
theValue = kp2(500);
  3 Comments
Nitesh
Nitesh on 3 Sep 2015
kp2 is a variable name i have given for proportional gain 2 (kp2) and im am doing some computation using matlab (linked with simulink) from where im plotting the scattered blue line values which is computed 5000 times.... (this value is pulled from the simulink when it is simulated.. something like online simulation)
so im using lsline command to find the best fit of the scatter kp2 value for 5000 cycles. I need the final value of the simulation present on the best fit line(red line) as the 5000 marked 'X'
when i use
y = get(d,'Ydata')
i get data at point 0 and 6000....0.5364 is at t=0 and 0.4734 is at t=6000 of the axis. I need t=5000 for the d=lsline.
Hope this will clear your doubt and you can help me. Please help and suggest. Thank you.

Sign in to comment.

Categories

Find more on View and Analyze Simulation Results 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!