Matlab动态传奇/传奇“坚持”就像行为一样

时间:2016-03-24 14:58:38

标签: matlab plot matlab-figure

只想添加更多数据做一个图例而不删除它。就像一个传奇“坚持”

示例

plotData =绘图数据数组,如plotData(i)= plot(...

N = plotData的大小。

代码

for i = 1:N
   str =  sprintf('My plot y %d', i);
   %legendData(:,i) = [plotData; str]; %#ok<SAGROW>
   %[~,~,~,current_entries] = legend;
   %legend([current_entries [plotData; str]]); no sucess here

   % This command will erase the previous one. 
   legend(plotData,str);
end

legend([plotX1,plotX2],'x 1','x 2');

我想我可以存储循环中的图例信息并将其添加到最后一行,例如:

legend(DATAFROMLOOP?? [plotX1,plotX2],'x 1','x 2');

这是一个可能的解决方案,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:2)

您想要设置绘图对象的DisplayName属性,然后在绘制完所有内容后调用legend一次。 legend会自动从DisplayName属性中检索字符串以填充图例。

hplot1 = plot(rand(10,1), 'DisplayName', 'plot1');
hplot2 = plot(rand(10,1), 'DisplayName', 'plot2');

legend([hplot1, hplot2]);

enter image description here

您可以轻松地将其合并到一个循环中:

% Create 10 plots within a loop
N = 10;

% Pre-allocate graphics objects
hplots = gobject(N, 1);

for k = 1:N
    hplot(k) = plot(rand(10, 1), 'DisplayName', sprintf('My plot y %d', k));
end

legend(hplot);

enter image description here

相关问题