Get intermediate data from plot

Hello;
I'm having problems with obtaining intermediate data from a Figure (plot). I have two vectors (Recorregut & Velocitat) which I have represented in the following figure as if I was working with Excel.
The thing is I have "x data" such as 24.54, 35.62, etc. and I would like to know the intermediate "y-data" value for every value of a vector x (x values starting at 0 and ending at 1700 with intervals of 2 for instance, that is to say, pieces of data which I do not know but I see in the figure) that Matlab plots in spite of the fact that I know that the spline that Matlab shows is not "real" data.
Is there a way to obtain all this data in a vector or in a matrix? If you attach some code I would be eternally grateful.
Thank you so much.
Yours
Catriona

 Risposta accettata

Use the interp1 function. Assuming ‘Recorregut’ is your independent variable and ‘Velocitat’ is your independent variable, you can get any intermediate variable.
For example:
Recorregut_i = 0 : 2 : 1700;
Velocitat_i = interp1(Recorregut, Velocitat, Recorregut_i, 'linear');
The ‘Velocitat_i’ vector will have values for every value of ‘Recorregut_i’.

5 Commenti

Catri Ona
Catri Ona il 19 Feb 2017
Thank you for your answer Star Strider
It does not work to me, it appears the following message
Error using griddedInterpolant
The grid vectors are not strictly monotonic increasing.
Error in interp1 (line 186)
F = griddedInterpolant(X,V,method);
Maybe it is because it is a strange plot?
Try
Velocitat_i = 0 : 2 : 1700;
Recorregut_i = interp1(Velocitat, Recorregut, Velocitat_i, 'linear');
Google translates Recorregut as being the Catalan word for 'tour', but unfortunately we are not familiar enough with the language to guess which one corresponds to 'x' and which corresponds to 'y'
I do not know which of your variables is your x-variable and which is your y variable.
Do this:
x_i = 0 : 2 : 1700;
y_i = interp1(x, y, x_i, 'linear');
Every value of ‘x_i’ should correspond to every value of ‘y_i’.
I do not have your data, so I can only guess that your ‘x’ is monotonically increasing (that is, every x(i+1)>x(i) as is required for the interp1 function). If that is not the situation you cannot use the interpolation functions.
Catri Ona
Catri Ona il 19 Feb 2017
Now that worked perfect guys! Thank you so much, this will save me a lot of time doing my Final Work's Degree! You are awesome!
Cat
Our pleasure!
Best of luck with your research! Congratulations on your degree!

Accedi per commentare.

Più risposte (1)

Once you have the plot,
ax = gca;
L = findobj(ax, '-property', 'XData');
x = get(L, 'XData');
y = get(L, 'YData');
dup_x = find(x(1:end-1) == x(2:end)) + 1;
x(dup_x) = [];
y(dup_x) = [];
wanted_x = 0 : 2 : 1700;
wanted_y = interp1(x, y, wanted_x), 'linear', 'extrap';
This does not depend upon whether Recorregut or Velocitat is x or y: it assumes that you used the proper one when you created the plot.
The dup_x processing is to delete the points that correspond to duplicated x, as I notice that the last x is exactly the same as the second-last x. As well, the second x might perhaps be the same as the first x.
I know that the spline that Matlab shows is not "real" data.
MATLAB never automatically splines when you plot(). MATLAB draws straight line segments between the points that it is given. The vertical line at the end cannot happen with a spline.

Community Treasure Hunt

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

Start Hunting!

Translated by