【发布时间】:2010-12-03 18:09:42
【问题描述】:
我有这个情节
我需要在 用户输入的 x 轴上的一点制作一条直线 垂直 线并显示交点的坐标这条垂直线与我的情节。
如何在 MATLAB 中做到这一点?
例如:用户输入 1020,然后将在 1020 处绘制一条垂直直线,在某个点与绘图相交,并以某种方式显示该点的坐标。
【问题讨论】:
标签: matlab graph plot intersection
我有这个情节
我需要在 用户输入的 x 轴上的一点制作一条直线 垂直 线并显示交点的坐标这条垂直线与我的情节。
如何在 MATLAB 中做到这一点?
例如:用户输入 1020,然后将在 1020 处绘制一条垂直直线,在某个点与绘图相交,并以某种方式显示该点的坐标。
【问题讨论】:
标签: matlab graph plot intersection
一种方法是使用GINPUT 函数以图形方式使用鼠标选择一个点。假设您绘制的数据存储在变量data 中,下面的代码应该可以做您想做的事情。
set(gca,'XLimMode','manual','YLimMode','manual'); % Fix axes limits
hold on;
[x,y] = ginput(1); % Select a point with the mouse
x = round(x); % Round x to nearest integer value
y = data(x); % Get y data of intersection
plot([x x],get(gca,'YLim'),'k--'); % Plot dashed line
plot(x,y,'r*'); % Mark intersection with red asterisk
disp('Intersection coordinates:');
disp([x y]); % Display the intersection point
以上假设图表的 x 值只是您正在绘制的数据数组的索引,从您上面显示的图表看来就是这种情况。
【讨论】:
尝试类似:
x = 1020;
% plot a vertical line
ylimits = get(gca, 'YLim');
hold on;
plot([x x], ylimits, 'k');
% mark the intersection with the plot
plot(x, data(x), 'ro');
annot = sprintf('Intersection: x=%f, y=%f', x, data(x));
text(x, data(x), annot);
代码未经测试,假设您的图形是当前图形,绘制的数据存储在数组“data”中,并且原始绘图是在未指定额外 x 向量的情况下完成的。
【讨论】:
您也可以使用函数hline和vline,,可从以下网址下载:http://www.mathworks.com/matlabcentral/fileexchange/1039-hline-and-vline
它们对你的作用几乎相同。
【讨论】: