How do I underline in fprintf?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Stephen23
le 18 Avr 2016
Modifié(e) : Stephen23
le 18 Avr 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 commentaires
Stephen23
le 18 Avr 2016
Modifié(e) : Stephen23
le 18 Avr 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.
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!