MATLAB: Break title into multiple lines

title;multiple line;

Hi,
When I am adding a title to a graph, is there a way to break the title into multiple lines if it is too long to be fit in just one line?
title('1st line\n2ndline')
I found the usual \n sequence in C/C++ is not useful here, nor does
title('1st line{\n}2ndline')
which I used {} brace to attempt a TEX interpretation. Could anyone tell me how multi-line title can be achieved?
Bob

Best Answer

You can create a multiline tile using either a cell array or a string array. Each element in the array corresponds to a separate line of text. Here’s how to do it with a cell array:
plot(1:10)
title({'You can do it','with a cell array'})
Here’s how to do it with a string array:
plot(1:10)
title(["You can do it","with a string array too"])
If you’re looking to create a subtitle, then starting in R2020b, you can pass a second line of text to the title function to create a subtitle.
title('A Nifty Title','A Clever Subtitle'])
Or you can call the title and subtitle functions separately.
title('A Nifty Title')
subtitle('A Clever Subtitle')