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

46 ビュー (過去 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
Alberto 2015 年 6 月 17 日
Multiline can be done using this syntax:
figure;
str=['Line1', '\n', 'Line2'];
s=sprintf('Hello\nGoodbye');
title(s)

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:end+1}=...
{ha.Title.String{end}(1:YourWidthValue) ;...
ha.Title.String{end}(YourWidthValue:end)};
end
(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
LastSpaceIndex=II(end);
ha(end).Title.String={ha(end).Title.String(1:LastSpaceIndex-1) ;
ha(end).Title.String(LastSpaceIndex+1:end)};
end
if iscell(ha(end).Title.String)
while size(ha(end).Title.String{end},2)>NoCharPerLine
STR=ha(end).Title.String{end};
II=strfind(STR,' '); % find last occurence of a space
LastSpaceIndex=II(end);
ha(end).Title.String{end}=STR(1:LastSpaceIndex-1);
ha(end).Title.String{end+1}=STR(LastSpaceIndex+1:end);
end
end

Hannes
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 件のコメント
Micke Malmström
Micke Malmström 2018 年 8 月 15 日
sorry, I dont have that problem with the lables...
What is your problem?
Hannes
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
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 件のコメント
Hannes
Hannes 2018 年 8 月 16 日
Thx for the response. I create a large set of subplots with many scatter plots, which are arranged in a mxn matrix, where m is the amount of input parameters and n is the amount of output parameters. I did not proceed with your approach since I hesitate to guess or somehow automate the calculation of the position for these text objects..
Jan
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 Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by