在MATLAB图中用值标记点
问题描述:
以下命令的确用正方形标记了点,但未在其中放置值(例如,(21,0)
,...).
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')
我应该添加哪个参数,以便所有5
点值都出现在图中?
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.
答
您可以使用功能 CELLSTR 和 STRTRIM 将坐标值格式化为字符串单元格数组并使用 TEXT 函数显示它们:
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: