Break title into multiple lines?
显示 更早的评论
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
2 个评论
Kunal Bhatt
2016-12-27
thanks
Kunal Bhatt
2016-12-27
In case of ylabel into multiple lines (3-lines or more) use following code ylabel({'line1', 'line2','line3'},)
采纳的回答
更多回答(3 个)
Aryan Ritwajeet Jha
2019-10-29
Adding to the above answer(s) as I was having problems with inserting variables in multiline plot titles.
This code snippet:
title({
['Partial Discharge Magnitudes in Time Domain predicted for' ]
['n = ' num2str(npotential) ' and i = ' num2str(ipotential) ]
['Actual values being n = ' num2str(nactual) ' and i = ' num2str(iactual)]
});
will generate a title like in the given image:

3 个评论
Sivateja Maturu
2019-11-5
编辑:Sivateja Maturu
2019-11-5
@Aryan
Thank you for your answer, I had this problem of num2str value appearing in anpther line all the time, Now I know the solution using the box.
Aryan Ritwajeet Jha
2019-11-5
Sivateja Maturu, you're most welcome!
Bor Kos
2021-3-26
or you could use sprintf in this context:
multilineTitleWithData={sprintf('First line x=%f',a),sprintf('SecondLine n=%f j=%f',n,j)}
The quickest way to insert a newline is to use. I hope this was helpful
title("My exquisite title \newline and my beautiful variable" + num2str(var1) + ".")
If you're not sure where to break the line of text, you can use the textwrap function.
s is a long-ish title, 97 characters long.
s = "The quick brown fox jumped over the lazy dog. " + ...
"It was the best of times, it was the worst of times"
strlength(s)
How does it look if we use s on its own as the title of a plot?
figure
plot(1:10, 1:10);
title(s)
Not so good. The title is cut off at the beginning and end. Let's break it into roughly 40 character long chunks.
figure
plot(1:10, 1:10);
t = title(textwrap(s, 40));
How long are each of those lines?
s2 = t.String
strlength(s2)
The first two lines are pretty close to 40 characters long, and the last line has the rest of the characters.
类别
在 帮助中心 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


