Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark points with a value in a MATLAB plot

Tags:

plot

matlab

The following command does mark the points with a square, but it does not put a value in (for example, (21,0), ...).

X = [21 8 2 1 0]

Y = [0 1 2 3 4]

plot(X,Y,'k-s')

Which parameter should I add so all 5 point values come on the plot?

The values can't be typed one by one as they can change, because they are random numbers.

like image 489
Eric Avatar asked Apr 09 '11 18:04

Eric


People also ask

How do you add a label to a point in Matlab?

To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates. text(___, Name,Value ) specifies Text object properties using one or more name-value pairs.


1 Answers

You can display text on your plot by using the functions NUM2STR, CELLSTR, and STRTRIM to format the coordinate values into a cell array of strings and using the function TEXT to display them:

strValues = strtrim(cellstr(num2str([X(:) Y(:)],'(%d,%d)')));
text(X,Y,strValues,'VerticalAlignment','bottom');

And your plot will look like this for the sample data above:

enter image description here

like image 162
gnovice Avatar answered Oct 24 '22 20:10

gnovice