【问题标题】:How to plot vertical line with string in MATLAB如何在MATLAB中用字符串绘制垂直线
【发布时间】:2014-12-11 17:36:51
【问题描述】:

我有两个数组,叫theArray1 theArray2 of N,格式如下:

5 13 20 ..
bloladsa adsad rwerds ..

我想在 {5,13,​​20,..} X 值和字符串中添加一条垂直线

同样的X值会写在行的下半部分(不要太在意位置)

我什至不知道如何做到这一点,所以没有代码可以显示

编辑 我画的垂直线:

hx = graph2d.constantline(theArray1, 'LineStyle',':', 'Color',[.7 .7 .7]);
changedependvar(hx,'x');

现在我只需要在这些地方添加文本

【问题讨论】:

  • 您是说要在位置5kikokiko 的位置打印字符串blablavla 13 并在这些位置画一条垂直线?
  • 你确定你有一个数组而不是一个单元格吗?
  • 你是对的,我改变了问题,谢谢

标签: matlab plot


【解决方案1】:

你可以这样做:

A={5, 'blablavla'; 13,'kikokiko';20,'bibobibo'}
lengthOfLine = 10;
for n=1:size(A,1)
    x = repmat(A{n,1},[1,lengthOfLine]);
    y = 1:lengthOfLine;
    plot(x,y)

    text(x(1)+0.1,y(1)+0.1,A{n,2})
    hold on
end    
hold off

% Adjust the axis so that the lines are more visible
axis([0 25 0 15])


详情

遍历你的项目

for n=1:size(A,1)

生成 xy 值。重要的是xy 的长度相同。我们使用repmat 重复一个值,例如十次。

x = repmat(A{n,1},[1,lengthOfLine]);
y = 1:lengthOfLine;

一个示例输出是

x = [   20   20   20   20   20   20   20   20   20   20];
y = [    1    2    3    4    5    6    7    8    9   10];

这将绘制一条垂直线和 x = 20。

绘制xy

plot(x,y)

在绘图中添加文本。文本的坐标将参考坐标系,因此我将0.1 添加到第一个 x 值 x(1) 以便文本将出现在该行的右侧。

text(x(1)+0.1,y(1)+0.1,A{n,2})
hold on

调整轴,使线条更明显

axis([0 25 0 15])

【讨论】:

  • thx 但正如我所补充的,更好的方法是使用 hx = graph2d.constantline(theArray1, 'LineStyle',':', 'Color',[.7 .7 .7]);改变依赖变量(hx,'x');我会尝试你的答案中的文本部分