在MATLAB中绘制表格并自动生成图例

时间:2016-07-30 16:44:35

标签: matlab plot

我正在MATLAB中读表如下:

  R = readtable('Recalls_All.csv','Delimiter',',','ReadVariableNames',false,...
'ReadRowNames',false);
  R.Properties.VariableNames = {'Features','R1','R5','R10','R20'};

此矩阵的第一列是' string'对应于不同算法的数据类型和其他4列是数字。我怎么能不使用循环,将行绘制为列的函数。

  • x_axis:
  • y轴:与不同算法相对应的行

显然,使用readtable()制作图片不是一项艰巨的任务。但是由于存在180行,我希望自动为每个绘图(算法)创建legend()

很赞赏很酷的想法。

2 个答案:

答案 0 :(得分:2)

这是一个选择:

% some arbitrary data:
R = table({'algorithm1' 'algorithm2' 'algorithm3'}.',randi(100,3,1),...
    randi(100,3,1),randi(100,3,1),randi(100,3,1),...
    'VariableNames',{'Features','R1','R5','R10','R20'});
ax = axes; % create axes
plot(ax,table2array(R(:,2:end)).'); % plot data
ax.XTick = 1:width(R); % limit X-axis ticks no. to columns
ax.XTickLabel = R.Properties.VariableNames(2:end); % get columns names
legend(table2cell(R(:,1))); % get algorithm names

结果:

Plotting a table

当然,您可以根据需要调整情节(线条,标记,限制等)。

答案 1 :(得分:1)

>> A = [1:5] .* [1:10]'    % example matrix
A =

    1    2    3    4    5
    2    4    6    8   10
    3    6    9   12   15
    4    8   12   16   20
    5   10   15   20   25
    6   12   18   24   30
    7   14   21   28   35
    8   16   24   32   40
    9   18   27   36   45
   10   20   30   40   50

>> VariableNames = {'name1', 'name2','name3','name4','name5'};
>> plot(1:size(A,1), A')
>> legend (VariableNames{:})

enter image description here