How to make long title for a plot with automatic line breaks?
Mostrar comentarios más antiguos
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.
Respuestas (5)
Micke Malmström
el 19 de Feb. de 2018
Editada: Micke Malmström
el 19 de Feb. de 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:
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 comentario
Mr M.
el 21 de Feb. de 2018
Micke Malmström
el 23 de Mayo de 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
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
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 comentarios
Micke Malmström
el 15 de Ag. de 2018
sorry, I dont have that problem with the lables...
What is your problem?
Hannes
el 15 de Ag. de 2018
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
el 15 de Ag. de 2018
0 votos
This is a job for the textwrap command. Create an uicontrol('Style', 'text') at the wanted position and provide its handle to textwrap.
4 comentarios
Hannes
el 15 de Ag. de 2018
I was hoping for a more generic solution without defining a position. At the moment I create a cell of strings and use it in the ylable:
ylabelname=strcat(A.Name(row),' _ ',A.Var(row),' (',A.Unit(row),')');
ylabel(ylabelname,'FontSize',9 , 'Interpreter', 'none', 'FontWeight','bold');
How do I now tell the label stay in a certain predefined width?
Jan
el 16 de Ag. de 2018
You do want to define a width, but not a position? Unfortunately Matlab cannot guess, what you want. Defining a uicontrol object of the style 'text' allows you to let textwrap do exactly, what you want. Therefore I suggest to use this solution instead of hoping for a more generic one. It is only 2 or 3 lines of code. You can even use this uicontrol only to let textwrap do its work, get its output and use it to call ylabel.
I've written a "more generic" solution, which is faster than textrwap. It has 350 lines of code.
Hannes
el 16 de Ag. de 2018
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
el 16 de Ag. de 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.
Categorías
Más información sobre Title en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!