Formatting Vectors using fprintf

7 Ansichten (letzte 30 Tage)
Zayd
Zayd am 17 Mär. 2025
Kommentiert: Zayd am 19 Mär. 2025
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 17 Mär. 2025
Bearbeitet: Stephen23 am 17 Mär. 2025
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 Kommentar
Zayd
Zayd am 19 Mär. 2025
Thank you! This works the most compactly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Voss
Voss am 17 Mär. 2025
Bearbeitet: Voss am 17 Mär. 2025
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]

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by