0
votes

Lets consider I am given a plot and I do not have its x and y vectors but I would like to extract them from the plot in Matlab. Also I am interested to know the increment of data (step size) in both horizontal and vertical axis(x and y axis). I was thinking of using :

h=gca             % Get current axis
X=get(h,'xdata');
Y=get(h,'ydata');
stepsize=X(2)-X(1);

But these command produce an error message that : xdata and ydata are not accessible property of axis. Any suggestion how to find the x and y vectors for any given curve.

2

2 Answers

1
votes

If I understand correctly, these are the two things you want to know:

  1. You have a figure containing a plot of some arbitrary 2d line, whose x_vec, y_vec are unknown to you and you want to extract them from the figure\axes.
  2. You want to get the xtick and ytick positions used in the figure you have.

The reason your code does not work, is because you're trying to access a property of the axes, whereas what you want to access is the property of the line (i.e. the curve in the plot).

To solve your first problem, you can resort to the following methods:

Manual: using the edit plot figure tool you can get to the XData and YData properties of the line, in the following manner:

How to get to a line's XData manually

Programmatic: you need to find the handle (i.e. pointer) to the line, and then use your code on that handle (and not on gca):

%// If there's only one entity (child) in the axes:
hLine = get(gca,'Children');
%// If there's more than one child:
hChildren = findobj(gca,'Type','line');
hLine = hChildren(1); %// Or any other logic you need to pick the correct line
%// Then comes your code:
xD = get(hLine,'XData'); yD = get(hLine,'YData');

For the second problem you can use gca to get XTick and YTick:

xT = get(gca,'XTick'); yT = get(gca,'YTick');

To get the step size I'd suggest simply using diff().

0
votes

I'm not sure I quite understand your question. You mean get x and y data of a curve? If yes, then maybe it'll help looking into 'ginput'.

For example, picking 10 points from a figure window you can use the following command

[x,y] = ginput(10)