How to use ' fprintf ' to display vector

256 visualizaciones (últimos 30 días)
LUAN NGUYEN
LUAN NGUYEN el 12 de Oct. de 2018
Comentada: Walter Roberson el 23 de Abr. de 2023
fprintf(.......)
% The result :
==> The vector P is: [6, 7, 3.1, 0 , 4.6, 8]

Respuestas (3)

Image Analyst
Image Analyst el 12 de Oct. de 2018
Try this:
P = [6, 7, 3.1, 0 , 4.6, 8];
fprintf('The vector P is: [');
fprintf('%g ', P);
fprintf(']\n');
  3 comentarios
Walter Roberson
Walter Roberson el 27 de Dic. de 2020
(note: no commas between elements as the original poster wanted.)
Image Analyst
Image Analyst el 28 de Dic. de 2020
If commas are wanted:
P = [6, 7, 3.1, 0 , 4.6, 8];
fprintf('The vector P is: [');
fprintf('%g, ', P(1:end-1));
fprintf('%g]\n', P(end));
Shows
The vector P is: [6, 7, 3.1, 0, 4.6, 8]

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 12 de Oct. de 2018
If it must be done with one fprintf(), then dynamically generate the format.
fmt = ['The vector P is: [', repmat('%g, ', 1, numel(P)-1), '%g]\n'];
fprintf(fmt, P)

Amit
Amit el 23 de Abr. de 2023
fprientf (‘The Integral value using trapezoidal rule :%.4f\n’,integral_value)
  1 comentario
Walter Roberson
Walter Roberson el 23 de Abr. de 2023
This will produce unexpected results for vectors

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by