Simple question regarding feld width in matlab

If saw a question in the internet which asks to write a segment code that prompt the user for a charcter and will print it twice; once left-justified in a field width of 5, and again right-justified in a field width of 3:
Here is my working:
ask=input('Enter a charcter: ','s');
fprintf('First time: %c\n',ask);
fprintf('First time:%c \n',ask);

3 件のコメント

Stephen23
Stephen23 2018 年 6 月 23 日
編集済み: Stephen23 2018 年 6 月 23 日
@ Sultan Al-Hammadi: your code does not make any attempt to change the fieldwidth or at justification. How to justify and change the fieldwidth is explained in the fprintf help.
What is your question for us?
Sultan Al-Hammadi
Sultan Al-Hammadi 2018 年 6 月 23 日
編集済み: Walter Roberson 2018 年 6 月 23 日
ask=input('Enter a charcter: ','s');
fprintf('First time:%5c\n',ask);
fprintf('Second time:%0.3c\n',ask);
I think that the first one is correct but the second fprintf is not.
Walter Roberson
Walter Roberson 2018 年 6 月 23 日
編集済み: Walter Roberson 2018 年 6 月 23 日
Neither one are correct. Your first one does not left justify and the right one does not print the character.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 6 月 23 日

0 投票

Use %-5c and %3c format items

カテゴリ

タグ

質問済み:

2018 年 6 月 23 日

編集済み:

2018 年 6 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by