formatting a table

2 次查看(过去 30 天)
Sean Smith
Sean Smith 2011-9-24
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

采纳的回答

Walter Roberson
Walter Roberson 2011-9-24
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', ....)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by