Benjamin Alonso Tan
Benjamin Alonso Tan

Reputation: 33

Extracting data points from a plot on a finer scale

I want to extract data from a plotted graph I have in matlab. As such, I did the following:

f = openfig('spline.fig');
xdata = get(gco, 'xdata');
ydata = get(gco, 'ydata');

This does give me the data points of x and y respectively, but with a step of 0.5 between each points (e.g 1, 1.5, 2, 2.5...). I was hoping to get finer data points than that (e.g 1, 1.1, 1.2, 1.3, 1.4...), and the corresponding y-coordinates to these x-coordinates. How can I do this?

Upvotes: 1

Views: 1324

Answers (1)

Fraukje
Fraukje

Reputation: 683

The way you got your ydata didn't work for me, I would use:

open testfigure.fig 
D = get(gca, 'Children');
ydata = get(D, 'YData'); 

The ydata I obtained contains the original y-data used to plot the figure.

Now, if you need a finer resolution in the data you will have to interpolate that yourself. Here's short example of how to interpolate this ydata to a resolution of 0.1:

  1. Define new x values xi to find y values for

    xi = 0:0.1:10;
    
  2. Interpolate ydata to find new yi values at xi:

    yi = interp1(xdata, ydata, xi); %// Using the default "linear"
    

You should read up on the kind of method you want to use (e.g. nearest neighbour, spline), this depends on your data and requirements.

Upvotes: 6

Related Questions