Borrar filtros
Borrar filtros

Help with printing output

1 visualización (últimos 30 días)
Nazarudin Iqzuan Marzulino
Nazarudin Iqzuan Marzulino el 2 de Jun. de 2020
Comentada: Nazarudin Iqzuan Marzulino el 2 de Jun. de 2020
I was trying to print some output that looks like this
Hours Linear Interpolation Nearest Interpolation
13.00 74.52 77.40
22.00 30.02 36.30
41.00 22.68 22.00
...and this is my coding...
disp(' Hours Linear Interpolation Nearest Interpolation')
fprintf(' %.2f %.2f %.2f\n', hours2(1), x(1), y(1))
fprintf(' %.2f %.2f %.2f\n', hours2(2), x(2), y(2))
fprintf(' %.2f %.2f %.2f', hours2(3), x(3), y(3)
I got the output that I want, but is there a way to make the coding looks more beautiful? and I also needed to add space manually to the data(value) so that it alligns with its column in which I thinks that it is kinda ineffecient.

Respuesta aceptada

Stephen23
Stephen23 el 2 de Jun. de 2020
Editada: Stephen23 el 2 de Jun. de 2020
Instead of adding spaces manually you can set the fieldwidth:
hours = [13,22,41];
x = [74.52,30.02,22.68];
y = [77.40,36.30,22.00];
fprintf(' Hours Linear Interpolation Nearest Interpolation\n')
fprintf(' %6.2f %8.2f %24.2f\n',[hours;x;y])
Prints:
Hours Linear Interpolation Nearest Interpolation
13.00 74.52 77.40
22.00 30.02 36.30
41.00 22.68 22.00
  1 comentario
Nazarudin Iqzuan Marzulino
Nazarudin Iqzuan Marzulino el 2 de Jun. de 2020
Thank you for the help! This is an interesting method!

Iniciar sesión para comentar.

Más respuestas (1)

madhan ravi
madhan ravi el 2 de Jun. de 2020
fprintf(' %.2f %.2f %.2f\n', [hours2(:).'; x(:).'; y(:).']) % Look documentation for adding space notation

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by