How to make long title for a plot with automatic line breaks?
99 views (last 30 days)
Show older comments
Mr M. on 17 Jun 2015
Commented: Jan on 16 Aug 2018
I would like to use a very long title, which should have lots of line breaks, but I want to put line breaks automatically to achieve a kind of justified paragraph visually.
Alberto on 17 Jun 2015
Multiline can be done using this syntax:
str=['Line1', '\n', 'Line2'];
Micke Malmström on 19 Feb 2018
Edited: Micke Malmström on 19 Feb 2018
Write code to
- Get the axis width (ha.Position), and fontsize (ha.Title.FontSize) in pixels (where ha is the handle to the axis of interest)
- If you use fixed width font then it's easy to break the line with something like:
(have not debugged it...)
an even better way is to search backwards for the first space before your YourWidthValue...
Is it not a bit strange that matlab cannot provide this functionallity built in?
Micke Malmström on 23 May 2018
ha=gca; NoCharPerLine= 78; % or whatever you want...
if ismatrix(ha(end).Title.String) && size(ha(end).Title.String,2)>NoCharPerLine
II=strfind((ha(end).Title.String(1:NoCharPerLine)),' '); % find last occurence of a space
II=strfind(STR,' '); % find last occurence of a space
Edited: Hannes on 15 Aug 2018
Thanks Micke, that helped a lot!. Now I try to adapt your code to axes labels, but struggle with that task. Do you have this already available?
The ylabel/xlabel don't have a title.string option to work with. So, I cannot just apply your code, it would need further re-coding, am I right?
Jan on 15 Aug 2018
This is a job for the textwrap command. Create an uicontrol('Style', 'text') at the wanted position and provide its handle to textwrap.
Jan on 16 Aug 2018
@Hannes Lück: This is a question from 2015. Prefer to open a new thread an explain your question exactly. Provide some example code to demonstrate the problem, then it is much easier to write a solution.
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!