formatting a table

7 views (last 30 days)
Sean Smith
Sean Smith on 24 Sep 2011
I made this table and I am having trouble formatting it. I want all the decimals in each column to line up and have each value to 2 decimal plays but because of the negatives and different number of values before the table it screws everything up. Whats a better way to format this? Thanks. P.S. Am I even using meshgrid correctly? It is a homework problem and I need to use meshgrid and vectorized calculations. My values are all right just not sure if making the variables x1, x2, x3 is necessary.
clear
t=0:10;
v=[10, -10, 20];
a=[2, 4, -6];
[V,A,T]=meshgrid(v,a,t);
x=V.*T+0.5.*A.*T.^2;
x1=x(1,1,:);
x2=x(2,2,:);
x3=x(3,3,:);
fprintf('\n');
fprintf('------------------------------------\n');
fprintf(' v = 10 v = -10 v = 20\n');
fprintf('t a = 2 a = 4 a = -6\n');
fprintf('------------------------------------\n');
for k=(1:11),
fprintf('%2d %3.2f %3.2f %3.2f\n', t(k), x1(k), x2(k), x3(k));
end

Accepted Answer

Walter Roberson
Walter Roberson on 24 Sep 2011
Instead of using a bunch of spaces and then %3.2f, use no spaces and after the % put the total number of columns the field should occupy including spaces. Continue to use the .2f part.
For example,
fprintf('%2d%15.2f%15.2f%15.2f\n', ....)

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!