How to make long title for a plot with automatic line breaks?

조회 수: 39 (최근 30일)
Mr M.
Mr M. . 2015년 6월 17일
댓글: Jan . 2018년 8월 16일
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.

답변 (5개)

Alberto 2015년 6월 17일
Multiline can be done using this syntax:
str=['Line1', '\n', 'Line2'];

Micke Malmström
Micke Malmström 2018년 2월 19일
편집: Micke Malmström 님. 2018년 2월 19일
Write code to
  1. Get the axis width (ha.Position), and fontsize (ha.Title.FontSize) in pixels (where ha is the handle to the axis of interest)
  2. If you use fixed width font then it's easy to break the line with something like:
while length(ha.Title.String{end})>YourWidthValue
{ha.Title.String{end}(1:YourWidthValue) ;...
(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?
  댓글 수: 1
Mr M.
Mr M. 2018년 2월 21일
:) why in every years there are lots of updates and MATLAB is old, I think its time to format a title as we want, its not a big deal

댓글을 달려면 로그인하십시오.

Micke Malmström
Micke Malmström 2018년 5월 23일
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
ha(end).Title.String={ha(end).Title.String(1:LastSpaceIndex-1) ;
if iscell(ha(end).Title.String)
while size(ha(end).Title.String{end},2)>NoCharPerLine
II=strfind(STR,' '); % find last occurence of a space

Hannes 2018년 8월 15일
편집: Hannes 님. 2018년 8월 15일
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?
  댓글 수: 2
Hannes 2018년 8월 15일
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 2018년 8월 15일
This is a job for the textwrap command. Create an uicontrol('Style', 'text') at the wanted position and provide its handle to textwrap.
  댓글 수: 4
Jan 2018년 8월 16일
@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.

댓글을 달려면 로그인하십시오.


Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by