MATLAB: How to make a figure legend multiple lines

MATLAB

The string for my legend is too long to fit into one line. I would like to split it into multiple lines.

Best Answer

To split legend to multiple lines, use the "newline" function or the ASCII character 10 for new line, for example:
>> plot(sin(1:10),'b')
>> hold on
>> plot(cos(1:10),'r')
>> legend({['blue' char(10) 'line'],'red line'})
With MATLAB "strings" you can use the following notation:
>> legend({ "blue" + newline + "line", 'red line'})
or
>> legend({ strcat("blue", string(newline), "line"), 'red line' })
In addition to the MATLAB command line, the legend can also be updated interactively in the figure itself to include multi-line legend entries.