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 commentaires

Kunal Bhatt
Kunal Bhatt le 27 Déc 2016
thanks
Kunal Bhatt
Kunal Bhatt le 27 Déc 2016
In case of ylabel into multiple lines (3-lines or more) use following code ylabel({'line1', 'line2','line3'},)

Connectez-vous pour commenter.

 Réponse acceptée

the cyclist
the cyclist le 29 Août 2023
Modifié(e) : MathWorks Support Team le 29 Août 2023

115 votes

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 commentaires

Bob Li
Bob Li le 16 Jan 2012
cyclist,
I got it, thanks very much.
Bob
Jeong Jinu
Jeong Jinu le 8 Sep 2018
Awesome example
Yonatan Marin
Yonatan Marin le 21 Nov 2018
Déplacé(e) : Dyuman Joshi le 11 Sep 2023
Hello Cyclist!
I get a sideeffect, when writing the legend this way. The lines I plot become double...Why does this happen?
I'm using a R2017b edition of Matlab.
Best regards,
Yonatan
Timothy Sawe
Timothy Sawe le 2 Mai 2020
True that
Shawn Prosky
Shawn Prosky le 24 Nov 2020
thank you!
Wakeel Mohammed
Wakeel Mohammed le 10 Jan 2021
Thank you! Thanks a lot
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco le 11 Juin 2021
Modifié(e) : Giuseppe Degan Di Dieco le 11 Juin 2021
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
Paulo Providencia le 25 Mai 2023
Note the bug in your sugestion above for the subtitle option (the extra ] close to the end).

Connectez-vous pour commenter.

Plus de réponses (3)

Aryan Ritwajeet Jha
Aryan Ritwajeet Jha le 29 Oct 2019

24 votes

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:
untitled.jpg

3 commentaires

Sivateja Maturu
Sivateja Maturu le 5 Nov 2019
Modifié(e) : Sivateja Maturu le 5 Nov 2019
@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
Aryan Ritwajeet Jha le 5 Nov 2019
Sivateja Maturu, you're most welcome!
or you could use sprintf in this context:
multilineTitleWithData={sprintf('First line x=%f',a),sprintf('SecondLine n=%f j=%f',n,j)}

Connectez-vous pour commenter.

Josef
Josef le 6 Mai 2023
Modifié(e) : Josef le 6 Mai 2023

2 votes

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"
s = "The quick brown fox jumped over the lazy dog. It was the best of times, it was the worst of times"
strlength(s)
ans = 97
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
s2 = 3×1 cell array
{'The quick brown fox jumped over the ' } {'lazy dog. It was the best of times, it '} {'was the worst of times' }
strlength(s2)
ans = 3×1
36 39 22
The first two lines are pretty close to 40 characters long, and the last line has the rest of the characters.

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by