问题
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