Assemble Legend for Many Curves

南楼画角 提交于 2021-01-29 16:53:13

问题


I need to assemble a Matlab/Octave legend for indexed curves, and I found the following example which seems to work well:

legend(strcat("curve ", num2str(1:2)))

Associates the labels "curve 1" and "curve 2" with the two curves given. However, if I need to add a different, non-indexed type of curve, the method above seems not to work anymore.

legend(strcat("curve ", num2str(1:2)),"another curve")

In the second example the first curve has for legend ["curve 1"; "curve 2"], and the second curve gets "another curve" for legend, while the last curve gets no legend. I think it has to do with the way legend interprets input, and I'm not able to get around it.


回答1:


Try assembling the legend as a cell array beforehand, and then using that as the legend input.

legendCell = cell.empty
for i = 1:2
   legendCell{i} = ['curve' num2str(i)];
end
legendCell{end+1} = 'another curve';
legend(legendCell);


来源:https://stackoverflow.com/questions/53418548/assemble-legend-for-many-curves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!