How to use ' fprintf ' to display vector

조회 수: 131 (최근 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 CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by