Help formatting text inside sprintf()
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I need the authors name of a poem right-aligned at the bottom of the text, however I'm unsure how to do this.
My poem should look like this:

however my code gets me this:

my code is:
poem = sprintf(['Oh! I have slipped the surly bonds of earth, \n'...
'And danced the skies on laughter-silvered wings; \n'...
'Sunward I\''ve climbed, and joined the tumbling \n'...
'mirth \n'...
'Of sun-split clouds, --and done a hundred things \n'...
'You have not dreamed of -- Wheeled and soared and \n'...
'swung \n'...
'\n'...
'High in the sunlit silence. Hov\''ring there \n'...
'I\''ve chased the shouting wind along, and flung \n'...
'My eager craft through footless halls of air... \n'...
'Up, up the long, delirious, burning blue \n'...
'I\''ve topped the wind-swept heights with easy grace \n'....
'Where never lark or even eagle flew -- \n' ...
'And, while with silent lifting mind I\''ve trod \n'...
'The high untrespassed sanctity of space. \n'...
'\n'...
'\t John Gillespie Magee, Jr']);
disp(poem);
0 Kommentare
Antworten (1)
Walter Roberson
am 3 Dez. 2018
Bearbeitet: Walter Roberson
am 3 Dez. 2018
you cannot do that with sprintf at all unless you are certain that you are using aa fixed width (monospaced) font.
If you are using aa fixed width font then code the other text excluding the signature as cell array of character vectors or as aa string array. find the display length of the longest line (tricky if you have combining accents or zero width characters.) Now display the text such as with
fprintf('%s\n', TheCell{:});
Now fprintf the signature using a %Ns format where N is replaced with the maximum length . One way
fprintf('%*s\n', maxlen, signature );
thiss uses an advanced fprintf feature . you could instead use
fprintf(sprintf('%%%ds\\n', maxlen), signature )
0 Kommentare
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!