Help formatting text inside sprintf()

5 ビュー (過去 30 日間)
Phil Hargreaves
Phil Hargreaves 2018 年 12 月 3 日
編集済み: Walter Roberson 2018 年 12 月 3 日
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);

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 12 月 3 日
編集済み: Walter Roberson 2018 年 12 月 3 日
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 )

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by