필터 지우기
필터 지우기

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