Formatting Vectors using fprintf

조회 수: 4 (최근 30일)
Zayd
Zayd 2025년 3월 17일
댓글: Zayd 2025년 3월 19일
How would one print on the command window a 3x1 vector labeled "R" such that the output line matches the format as shown below? Currently I am using a bunch of fprintf functions and manually spacing out the outputs to make it look right, but I want to know if there's a more compact method of outputting and formatting text into command line.

채택된 답변

Stephen23
Stephen23 2025년 3월 17일
편집: Stephen23 2025년 3월 17일
R = [6640.441; 0; 0];
C = compose('%7s[%10.3f km]',["";"R = ";""],R);
fprintf('%s\n','A) Position with respect to the earth in PQW coordinates:',C{:})
A) Position with respect to the earth in PQW coordinates: [ 6640.441 km] R = [ 0.000 km] [ 0.000 km]
  댓글 수: 1
Zayd
Zayd 2025년 3월 19일
Thank you! This works the most compactly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Voss
Voss 2025년 3월 17일
편집: Voss 2025년 3월 17일
R = [6640.441; 0; 0];
C = compose(' [ %8.3f km]',R);
C{2}([1 3]) = 'R=';
fprintf('%s\n',C{:});
[ 6640.441 km] R = [ 0.000 km] [ 0.000 km]

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by