Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab动态图例/图例“;“等一下”;相似行为_Matlab_Plot_Matlab Figure - Fatal编程技术网

Matlab动态图例/图例“;“等一下”;相似行为

Matlab动态图例/图例“;“等一下”;相似行为,matlab,plot,matlab-figure,Matlab,Plot,Matlab Figure,只想添加更多数据,在不删除图例的情况下创建图例。就像传说中的“等等” 样本: 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

只想添加更多数据,在不删除图例的情况下创建图例。就像传说中的“等等”

样本

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');
plotData
=绘图数据数组,如plotData(i)=绘图(

N
=绘图数据的大小

代码

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
legend
将自动从
DisplayName
属性检索字符串以填充图例

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

legend([hplot1, hplot2]);

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

% 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);