Simple question regarding feld width in matlab

4 visualizaciones (últimos 30 días)
Sultan Al-Hammadi
Sultan Al-Hammadi el 23 de Jun. de 2018
Editada: Walter Roberson el 23 de Jun. de 2018
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 comentarios
Sultan Al-Hammadi
Sultan Al-Hammadi el 23 de Jun. de 2018
Editada: Walter Roberson el 23 de Jun. de 2018
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 el 23 de Jun. de 2018
Editada: Walter Roberson el 23 de Jun. de 2018
Neither one are correct. Your first one does not left justify and the right one does not print the character.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 23 de Jun. de 2018
Use %-5c and %3c format items

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by