MATLAB Answers

How do I place a two-lined title, x-label, y-label, or z-label on my plot?

2.501 views (last 30 days)
I would like to create a multi-lined title, x-label, y-label or z-label. I would like to know if there are any functions in MATLAB that allow me to create a title that uses multiple lines.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 12 Jun 2017
Edited: MathWorks Support Team on 12 Jun 2017
You can create multi-line text using a cell array. For example, this code creates a title and an axis label with two lines.
plot(1:10)
title({'First line';'Second line'})
xlabel({'First line';'Second line'})
For more information about creating multiline text, see
  4 Comments
Sk. Alam
Sk. Alam on 30 Mar 2021 at 12:43
if I wish to insert a string in any lines how can I insert strings in a single line of xlabel?

Sign in to comment.

More Answers (2)

Karan Gill
Karan Gill on 31 Oct 2016
Replace spaces with "\newline".
labels = {'line1 line2','line1 line2','line1 line2'};
labels = cellfun(@(x) strrep(x,' ','\newline'), labels,'UniformOutput',false);
a = gca;
a.XTickLabel = labels;
Karan | Symbolic Math documentation
  4 Comments
RANJAN HALDER
RANJAN HALDER on 20 Oct 2020
Yes,Leilane I am facing the same problem too...Please someone help us to solve this problem

Sign in to comment.


Yogita Wable
Yogita Wable on 28 Mar 2020
try below options:
To create multiline text:
  • Use a cell array, where each cell contains a line of text, such as {'first line','second line'}.
  • Use a character array, where each row contains the same number of characters, such as ['abc'; 'ab '].
  • Use sprintf to create a string with a new line character, such as sprintf('first line \n second line'). This property converts strings with new line characters to cell arrays.
Text that contains only a numeric value is converted to a string using sprintf('%g',value). For example, 12345678 displays as 1.23457e+07.

Community Treasure Hunt

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

Start Hunting!

Translated by