alignment issue in fprint

조회 수: 2 (최근 30일)
Kareemulla Dudekula
Kareemulla Dudekula 2022년 1월 24일
댓글: Rik 2022년 1월 24일
My fprintf table is not aligned
P=[65.44; 87.12; 67.46; 79.51];
words1={'Moisture';'Fixed carbon';'Volatile matter';'Ash'};words2 = repmat({'wt%'},length(P),1);
D={words1,P,words2};
for k1 = 1:size(words1,1);
fprintf('%s \t %.2f \t %s \n',char(D{1}(k1)),D{2}(k1,:),char(D{3}(k1)))
end
excecutes as follows:
Moisture 65.44 wt%
Fixed carbon 87.12 wt%
Volatile matter 67.46 wt%
Ash 79.51 wt%
Could someone please help in figuring out how to align the strings and digits.

채택된 답변

yanqi liu
yanqi liu 2022년 1월 24일
P=[65.44; 87.12; 67.46; 79.51];
words1={'Moisture';'Fixed carbon';'Volatile matter';'Ash'};
words2 = repmat({'wt%'},length(P),1);
D={words1,P,words2};
for k1 = 1:size(words1,1);
fprintf('%20s \t %.2f \t %s \n',char(D{1}(k1)),D{2}(k1,:),char(D{3}(k1)))
end
Moisture 65.44 wt% Fixed carbon 87.12 wt% Volatile matter 67.46 wt% Ash 79.51 wt%
  댓글 수: 2
Kareemulla Dudekula
Kareemulla Dudekula 2022년 1월 24일
편집: Kareemulla Dudekula 2022년 1월 24일
Thanks very much! I think 20s is figured out through a series of trials?; I am wondering if there is a more elegant way to do it. Also, what if I had my P matrix as follows: P=[5.44; 87.12; 67.46; 9.51]; decimal places do not align...
Rik
Rik 2022년 1월 24일
It does, you just have to specify the total width you want:
P=[5.44; 87.12; 67.46; 9.51];
fprintf('data: %5.2f\n',P)
data: 5.44 data: 87.12 data: 67.46 data: 9.51

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

추가 답변 (1개)

KSSV
KSSV 2022년 1월 24일
P=[65.44; 87.12; 67.46; 79.51];
words1={'Moisture';'Fixed carbon';'Volatile matter';'Ash'};
words2 = repmat({'wt%'},length(P),1);
T = table(words1,P,words2)
T = 4×3 table
words1 P words2 ___________________ _____ _______ {'Moisture' } 65.44 {'wt%'} {'Fixed carbon' } 87.12 {'wt%'} {'Volatile matter'} 67.46 {'wt%'} {'Ash' } 79.51 {'wt%'}

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by