How to use ' fprintf ' to display vector

133 ビュー (過去 30 日間)
LUAN NGUYEN
LUAN NGUYEN 2018 年 10 月 12 日
コメント済み: Walter Roberson 2023 年 4 月 23 日
fprintf(.......)
% The result :
==> The vector P is: [6, 7, 3.1, 0 , 4.6, 8]

回答 (3 件)

Image Analyst
Image Analyst 2018 年 10 月 12 日
Try this:
P = [6, 7, 3.1, 0 , 4.6, 8];
fprintf('The vector P is: [');
fprintf('%g ', P);
fprintf(']\n');
  3 件のコメント
Walter Roberson
Walter Roberson 2020 年 12 月 27 日
(note: no commas between elements as the original poster wanted.)
Image Analyst
Image Analyst 2020 年 12 月 28 日
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]

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


Walter Roberson
Walter Roberson 2018 年 10 月 12 日
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 2023 年 4 月 23 日
fprientf (‘The Integral value using trapezoidal rule :%.4f\n’,integral_value)
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 4 月 23 日
This will produce unexpected results for vectors

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

カテゴリ

Help Center および File ExchangeOther Formats についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by