如何在MATLAB中用字符串绘制垂直线

时间:2014-12-11 08:34:45

标签: matlab plot

我有两个数组,让它以这种格式称为N的阵列aArray2:

5 13 20 ..
bloladsa adsad rwerds ..

我希望在我的情节中添加{5,13,​​20,..} X值的垂直线和字符串

在相同的X值中会写下行的下半部分(不要真正关心位置)

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

编辑 我绘制的垂直线:

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

现在我只需要在那些地方添加文字

1 个答案:

答案 0 :(得分: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])

enter image description here


<强>详情

循环浏览您的商品

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])
相关问题