Formatting Vectors using fprintf

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 日

1 投票

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 日

1 投票

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]

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

製品

リリース

R2024b

質問済み:

2025 年 3 月 17 日

コメント済み:

2025 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by