How to properly align output text in command window?

22 visualizzazioni (ultimi 30 giorni)
nikola
nikola il 23 Ago 2011
Risposto: Voss il 28 Dic 2021
I use fprintf to print data in command window ( A-data vector, f-data matrix)
for br=1:n
fprintf('\n %4.2f ',A(br));
fprintf(' %4.0f ',f(br,:));
end
and get this:
365.00 962 995 1027 1058 1089 1120 1150 1179
367.00 973 1006 1038 1070 1101 1132 1162 1192
296.50 635 656 677 698 719 739 759 778
96.50 67 70 72 74 76 78 80 82
but, I need it like (nice aligned)
365.00 962 995 1027 1058 1089 1120 1150 1179
367.00 973 1006 1038 1070 1101 1132 1162 1192
296.50 635 656 677 698 719 739 759 778
96.50 67 70 72 74 76 78 80 82
Any suggestions? tnx

Risposte (1)

Voss
Voss il 28 Dic 2021
One solution is to use enough characters in your fixed-width format, so that the strings that get fprintf'ed are exactly the same width:
A = [365 367 296.5 96.5];
f = [962 995 1027 1058 1089 1120 1150 1179; ...
973 1006 1038 1070 1101 1132 1162 1192; ...
635 656 677 698 719 739 759 778; ...
67 70 72 74 76 78 80 82];
n = size(f,1);
for br=1:n
fprintf('\n %6.2f ',A(br));
fprintf(' %6.0f ',f(br,:));
end

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by