How to use ' fprintf ' to display vector

Antworten (3)

Image Analyst
Image Analyst am 12 Okt. 2018

9 Stimmen

Try this:
P = [6, 7, 3.1, 0 , 4.6, 8];
fprintf('The vector P is: [');
fprintf('%g ', P);
fprintf(']\n');

3 Kommentare

Sergio LM
Sergio LM am 27 Dez. 2020
Thanks, great work.
Walter Roberson
Walter Roberson am 27 Dez. 2020
(note: no commas between elements as the original poster wanted.)
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]

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 12 Okt. 2018

3 Stimmen

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 am 23 Apr. 2023

0 Stimmen

fprientf (‘The Integral value using trapezoidal rule :%.4f\n’,integral_value)

Gefragt:

am 12 Okt. 2018

Kommentiert:

am 23 Apr. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by