代码之家  ›  专栏  ›  技术社区  ›  Mike

如何在Matlab中标记两个向量?

  •  3
  • Mike  · 技术社区  · 15 年前

    我有一个2列矩阵(称为 M plot 命令( plot(M) ). 我有两个问题:

    1. 我想在绘图上标记矩阵的每一行(即每个向量分量)。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Amro    9 年前

    M = cumsum(rand(10,2) - 0.5);
    x = 1:size(M,1);
    plot(x, M(:,1), 'b.-', x, M(:,2), 'g.-')
    legend('M1', 'M2')
    for i=x
        text(i+0.1, M(i,1), sprintf('%.2f', M(i,1)), 'FontSize',7, 'Color','b');
        text(i+0.1, M(i,2), sprintf('%.2f', M(i,2)), 'FontSize',7, 'Color','g');
    end
    

    plot

    或者,您可以使用:

    datacursormode()
    

    这将使用户能够 point and click on points

        2
  •  1
  •   Richie Cotton Joris Meys    15 年前

    您可能需要对此进行调整,以准确地获得标签的位置,但类似这样的操作可以实现这一目的。

    M = [1 2; 3 4; 5 6]
    plot(M)
    nrows = size(M, 1);
    ncols = size(M, 2);
    x = repmat(nrows - .3, 1, ncols);
    y = M(end, :) - .3;
    labels = cellstr([repmat('Col', ncols, 1), num2str((1:ncols)')]);
    text(x, y, labels)
    
        3
  •  0
  •   Jeff    15 年前

    xlabel('label')
    ylabel('label')
    

    这些参数还可以采用单元格参数,其中每一行都是新行。这很方便显示单位。标记图形上的每个点可以这样做:

    for i=1:length(M)
        text(M(i,1),M(i,2),'Label Text')
    end
    

    标签文本也可以是一个字符串变量,您可以使用sprintf进行编辑,并为每个点生成特殊字符串。