How to make two fprintf statements vertically align each other?

17 vues (au cours des 30 derniers jours)
look at these two big fprintf statements. How can I align data with title?
fprintf('Alt Temp TAS CAS IAS TrueTrack WindD WindV TrueHeading MagVar MagHeading GroundSpeed Dist. Time GPH Cruisefuel CruiseRPM\n' );
fprintf('%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f\n',al,te,ktasgph(1),cas,ias,tc,wd,wv,trueheading,mv,magheading,wcaAndgs(2),dist,time,ktasgph(2),cruisefuel,rpm);

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Jan 2016
fprintf(' Alt Temp TAS CAS IAS TrueTrack WindD WindV TrueHeading MagVar MagHeading GroundSpeed Dist. Time GPH Cruisefuel CruiseRPM\n' );
fprintf('%4.2f %4.2f %4.2f %4.2f %4.2f %9.2f %5.2f %5.2f %12.2f %6.2f %11.2f %11.2f %5.2f %4.2f %3.2f %10.2f %9.2f\n', al, te, ktasgph(1), cas, ias, tc, wd, wv, trueheading, mv, magheading, wcaAndgs(2), dist, time, ktasgph(2), cruisefuel, rpm);
If any of the headings are not as wide as are needed for the numbers to be shown, then add blanks before the heading word and increase the corresponding number after the % by that many blanks. For example you might notice that although 'Alt' is only 3 characters I used %4.2f, because the minimum characters that will be printed if you need two decimal places is 4, which is a leading digit (might be 0), a decimal point, and the two decimal places after the decimal point. If you needed 3 digits before the decimal point for Alt then you would add two more spaces before Alt in the heading and add 2 to the 4 to get %6.2f as the format for that item.
  1 commentaire
Mathidiot Superfacial
Mathidiot Superfacial le 7 Jan 2016
Modifié(e) : Mathidiot Superfacial le 7 Jan 2016
I finally fixed it, using '%-13s' to fix the titles and used %12.2f to fix the data.
if true
fprintf('%-13s','Alt', 'Temp','TAS','CAS','IAS','TrueTrack','WindD','WindV','TrueHeading','MagVar','MagHeading','GroundSpeed','Distance','Time(m)','GPH','Cruisefuel','CruiseRPM');
fprintf('\n%-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f %-12.2f\n',al,te,ktasgph(1),cas,ias,tc,wd,wv,trueheading,mv,magheading,wcaAndgs(2),dist,time,ktasgph(2),cruisefuel,rpm);
end

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 7 Jan 2016
In the format spec, you can specify the field width. For example,
'%8.2f'
will have field width 8, with 2 digits precision.

Catégories

En savoir plus sur Guidance, Navigation, and Control (GNC) dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by