Legend for variable number of plots
13 次查看(过去 30 天)
显示 更早的评论
Hi, I am plotting a variable 'y' whose size is 'm-by-n' against 'x' whose size is 'm-by-1'. So, I get 'n' plots on the current figure. Now, how can I write insert legend without knowing the value of n. I mean, how do I give dynamic input to legend function.
plot(x,y)
legend('Mode 1','Mode 2','Mode 3',...'Mode n')
0 个评论
采纳的回答
Guillaume
2016-3-9
legend(arrayfun(@(mode) sprintf('Mode %d', mode), 1:size(y, 2), 'UniformOutput', false))
Would be one way of doing it.
3 个评论
Guillaume
2016-3-9
编辑:Guillaume
2016-3-9
The arrayfun part builds a cell array of strings that is then passed to legend. It is equivalent to:
legendstrings = cell(1, size(y, 2)); %arrayfun automatically constructs a cell array the right size
for mode = 1:size(y, 2) %this is the second input of arrayfun
legendstrings{mode} = sprintf('Mode %d', mode); %1st input of arrayfun, sort of
end
legend(legendstrings);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!