How to properly align output text in command window?

9 views (last 30 days)
nikola
nikola on 23 Aug 2011
Answered: Voss on 28 Dec 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

Answers (1)

Voss
Voss on 28 Dec 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

Categories

Find more on Cell Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!