问题
As shown in the image below, the legend is too long for the plot window. what I want to do is:
1- To know how to split the legend over a second line?
2- To know how to shorten the red line in indicated in the legend. As you see, the legend contains a red line then (x1=......), I want to know whether there is any possibility to shorten that red line or to control its length.

回答1:
There are two possibilities to control the content of the legend:
Use of TeX commands: MATLAB parses the legend entries (and titles, labels etc.) with TeX. You can use most TeX features and symbols. You find some details in the MATLAB help pages, in the Interpreter section.
sprintf: The
sprintf
function works like allprintf
functions and outputs a string. It allows you to add control characters like\n
, insert variables etc. Have a look at the documentation for details.
In your case, you can use either of the following code snippets:
legend('x1 = x = ... \newline ...');
legend(sprintf('x1 = x = ... \n, ...'));
On your second question: I'm not aware of any possibility to change that line. Sorry.
回答2:
As for you second question - you can call legend with 2 output arguments:
[h,att] = legend('show')
And then use att.XData
property to manipulate this line.
plot(sin(-pi:0.1:pi),'r')
[h,att] = legend('show')
att(2).XData(2) = 0.2;
The result:
来源:https://stackoverflow.com/questions/27686180/how-to-customize-a-legend-in-matlab-plot-window