Reputation: 3881
I'm trying to display my values for a given vector within a plot. My code is:
x = [0.1 0.2 -0.1 4.1 -2 1.5 -0.1];
plot(x)
a = num2str(x(:));
b = cellstr(a);
c = strtrim(b);
text(x,y,c);
Its plotting the values, but they are scattered all over the place, and not sitting nicely next to each point on the graph.
Upvotes: 0
Views: 6035
Reputation: 12214
As I said in my comment above, calling plot
with a single vector input treats the values of the vector as the y-coordinates and their indices as the x-coordinates. Your provided x
vector contains negative numbers, but your plot
call only has one vector input, so there will be no negative x-coordinates in the plot (there are no negative indices in MATLAB).
Assuming your x
vector is your desired y-coordinate, the following example will provide the behavior I'm guessing you're expecting:
y = [0.1 0.2 -0.1 4.1 -2 1.5 -0.1];
x = 1:1:length(y);
plot(x,y)
a = num2str(x(:));
b = cellstr(a);
c = strtrim(b);
h = text(x,y,c);
Where h
is an array of object handles that you can use with get
and set
to query and modify the properties of each individual text object (like size, alignment, etc.).
Upvotes: 2