答案 0 :(得分:4)
我创建一个空变量,然后为每个图例条目添加字符串的单元数组元素。在代码中添加图表的位置附加图例条目是一种方便的方式来保持组织有序 - 否则您可能有空白或加倍的图例:
x = 1:4;
figure
hold all
L ='';
%L = cell(1,N);
for j = 1:length(x)
scatter(j,x(j),60,'filled');
L = [L,{num2str(j)}];
end
%additional plots are easily added or taken out, leaving the legend in tact:
scatter(rand,rand,90,'x','linewidth',3);
L = [L,'optional data a'];
scatter(rand,rand,90,'x','linewidth',3);
L = [L,'optional data b'];
legend(L);