How do I underline in fprintf?
12 views (last 30 days)
Show older comments
fprintf( 'Year No Balance(1st Jan) Interest(31st Dec) Total(31st Dec)')
I want it to display Year No as underlined same with all the other titles. It should display Year No Balance(1st Jan) Interest (31st Dec) Total (31st Dec) So everything that is bold should be underlined
2 Comments
Answers (1)
Stephen23
on 18 Apr 2016
Edited: Stephen23
on 18 Apr 2016
fprintf and sprintf do not create formatted text, they create simple strings of characters. This means no underline, no italic, no bold, etc., because these things only make sense with formatted text.
One easy work-around would be to simply create a new line of characters to print under the title string:
>> str = 'Year No Balance(1st Jan) Interest(31st Dec) Total(31st Dec)';
>> idx = ~isstrprop(str,'wspace'); % detect spaces
>> idy = idx | [idx(2:end),true]&[true,idx(1:end-1)]; % ignore single space
>> und = char(32*~idy); % define output spaces
>> und(idy) = '-'; % add whatever character to use as the underline
>> fprintf('%s\n%s\n',str,und) % print
Year No Balance(1st Jan) Interest(31st Dec) Total(31st Dec)
------- ---------------- ------------------ ---------------
2 Comments
Stephen23
on 18 Apr 2016
Edited: Stephen23
on 18 Apr 2016
I know what you want. I am telling you what you can actually do. You need to learn that text files only support plain text, "which represent only characters of readable material but not its graphical representation". This means plain text does not include any font information, such as size, typeface, slant, weight, strikethrough, or underline. It does not matter how much you want plain text to be like Word, it isn't. It does not matter how many times you tell us that you want underlined text, fprintf will not give it to you. Get used to it.
This also has nothing to do with MATLAB, this is simply how text files are defined. And also fprint(f) is the same in every programming language: it does not provide formatted text.
If you really really need underline then you can:
- use a text file and add a line of characters, like I showed you in my answer.
- write a simple text file and convert it to a word doc yourself.
- write a Word document using some third-party tool.
- write a markdown document (or any other markup language, e.g. XML).
- use a figure and the text command.
Obviously my answer is inadequate, so here are some other answers, all saying that fprintf/sprintf will not give formatted text:
If you are planning to print to the command window then there are some special cases that can be handled by MATLAB (color, bold and underline):
However you need to be aware that this method is entirely undocumented and is not standard fprintf behavior. Use at your own risk.
See Also
Categories
Find more on Entering Commands 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!