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.
If I understand correctly, these are the two things you want to know:
x_vec, y_vec
are unknown to you and you want to extract them from the figure\axes.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:
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()
.