Help formatting text inside sprintf()
조회 수: 16 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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 )
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!