フィルターのクリア

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 Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by