MATLAB Answers

Multi-line legend strings

570 views (last 30 days)
Matt Raum
Matt Raum on 10 Apr 2018
Answered: Daniel Svedbrand on 13 Feb 2019
I recently upgraded from R2011b to R2017a. In R2011b I was able to create legend entries with multiple lines e.g.:
figure;
plot(1,1,'o');
legend(sprintf('hello\nworld'));
In 2017a this no longer seems to be possible. The text displayed in the legend is the literal string "hello\nworld" and I get the following warning:
Warning: Error updating Legend.
Character vector must have valid interpreter syntax:
hello\nworld
Furthermore, the legend text overruns the bounding box that is drawn for the legend. In both R2011b and R2017a, the 'interpreter' property for the legend object defaults to 'tex'. I've tried using both 'none' and 'latex' as the R2017a interpreter but still get the same results.
Is there a way to have a multi-line legend string in R2017a?

  1 Comment

Fangjun Jiang
Fangjun Jiang on 10 Apr 2018
The legend displays correctly in R2017a in Windows 10.

Sign in to comment.

Answers (2)

Daniel Svedbrand
Daniel Svedbrand on 13 Feb 2019
use \newline instead of \n and skip "sprintf"
legend('hello\newlineworld');

  0 Comments

Sign in to comment.


the cyclist
the cyclist on 11 Apr 2018
Maybe you have a different interpreter?
If you do
figure;
plot(1,1,'o');
hL = legend(sprintf('hello\nworld'));
What do you get from
hL.Interpreter
? Mine is 'tex'

  0 Comments

Sign in to comment.