aligning data for fprintf

조회 수: 6 (최근 30일)
sermet OGUTCU
sermet OGUTCU 2021년 8월 4일
답변: darova 2021년 8월 4일
for j=1:2
[a(j),b(j)]=size(char(FileName(:,j))); % b=[38 38]
[c(j),d(j)]=size(char(FileName2(:,j))); % d=[38 38]
first_space(j)=b(j)+4;
second_space(j)=d(j)+8;
end
fprintf(fid,'%*s %*s\n',max(first_space),' G',max(second_space),' G');
fprintf(fid, '\n\n');
for j=1:2
fprintf(fid,'%s %.1f %s %.1f\n', FileName{:,j}, data1(j), FileName2{:,j}, data2(j));
end
The above codes created the output txt file with aligned output data if the length of the Filenames are the same. For example:
FileName =
1×2 cell array
{'COD0MGXFIN_20210890000_01D_05M_ORB.SP3'} {'COD0MGXFIN_20210900000_01D_05M_ORB.SP3'}
FileName2 =
1×2 cell array
{'COD0MGXFIN_20210920000_01D_05M_ORB.SP3'} {'COD0MGXFIN_20210930000_01D_05M_ORB.SP3'}
If these FileNames are used, there is no problem for the alignment. I attached the output file (output_aligned.txt)
But, when the length of Filenames are different, the alignment cannot be maintained with the above codes. For example:
FileName =
1×2 cell array
{'COD0MGXFIN_20210920000_01D_05M_ORB.SP3'} {'COD20714.EPH_M'}
FileName2 =
1×2 cell array
{'COD0MGXFIN_20210890000_01D_05M_ORB.SP3'} {'COD20720.EPH_M'}
When using the above FileNames, the output file doesn't aligned. I attached the output file (output_non_aligned.txt) created the second part of above codes. How can I modify the above codes to write consistently aligned output regardless of the length of FileName and FileName2?

채택된 답변

darova
darova 2021년 8월 4일
Here is ax example
a = rand(10,1)*1e4;
sprintf('a = %7.2f\n',a')
ans =
'a = 169.57 a = 5470.00 a = 6023.50 a = 7339.91 a = 2607.20 a = 630.21 a = 2342.76 a = 3390.90 a = 9812.91 a = 9146.70 '

추가 답변 (1개)

Yongjian Feng
Yongjian Feng 2021년 8월 4일
Use "\t" to create tabs?
  댓글 수: 1
sermet OGUTCU
sermet OGUTCU 2021년 8월 4일
Could you show me this modification using the above codes to be more specific?

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

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by