필터 지우기
필터 지우기

Problem vertically aligning data in a table using fprintf

조회 수: 8 (최근 30일)
D
D 2011년 10월 2일
I'm putting my meshgrid to a table and I'm trying to make it look more presentable. I can't seem to figure out how to get them to line up vertically. My current code is:
for i=1:11
fprintf('%2d %3.2f %3.2f %3.2f\n', t(i),X(i),X(i+11),X(i+22))
end
  댓글 수: 2
Jan
Jan 2011년 10월 2일
What are the input values, what do you get and what do you want to achieve?
D
D 2011년 10월 2일
t = 0:10;
v = [10, -10, 20];
a = [2, 4, -6];
[V,T] = meshgrid(v,t);
[A,T] = meshgrid(a,t);
X = ((V.*T) + ((1/2).*A.*(T.^2)));
I get various values that are of different lengths, some are even negative, so that the columns aren't aligned vertically. I'm trying to make it so the data is aligned and the value doesn't look like
"44 11"
"-22 23"
want it to look like
" 44 11"
"-22 23"

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2011년 10월 2일
Do not put any spaces in your format. Instead, increase the "3" in your "%3.2f" to indicate the total number of characters to the right that you want the right edge of that column to be over by. For example, with %15.2f the right-hand side of the numbers would be the 15th character to the right of the previous column.
(Note: more characters will be used if required by the data value, such as if you were to try to print 1E42 with a 15-character fixed-point format.)

추가 답변 (1개)

Image Analyst
Image Analyst 2011년 10월 2일
Check your fonts: File/Preferences/Fonts. Maybe you're using a proportional font when you need a monospaced font.
  댓글 수: 1
D
D 2011년 10월 2일
The font is monospaced. It's the length of the data values that are different. some are positive and some negative.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by