Break title into multiple lines?
조회 수: 2,635 (최근 30일)
이전 댓글 표시
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일
In case of ylabel into multiple lines (3-lines or more) use following code ylabel({'line1', 'line2','line3'},)
채택된 답변
the cyclist
2023년 8월 29일
편집: MathWorks Support Team
2023년 8월 29일
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')
Refer to the documentation for an example of a multiline title:
댓글 수: 8
Giuseppe Degan Di Dieco
2021년 6월 11일
편집: Giuseppe Degan Di Dieco
2021년 6월 11일
Dear The Cyclist,
thanks for your tip, still useful in 2021.
It is really impressive how many things can be done with cell arrays.
It helped me in producing the attached graph, best!
Paulo Providencia
2023년 5월 25일
Note the bug in your sugestion above for the subtitle option (the extra ] close to the end).
추가 답변 (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
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)}
Steven Lord
2023년 5월 6일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!