필터 지우기
필터 지우기

Writing a string that fills to the left

조회 수: 5 (최근 30일)
James Browne
James Browne 2021년 5월 23일
댓글: James Browne 2021년 5월 25일
Hi,
I have data in an array wich I want to output into a text file to match the way another program writies it.
The program seems to write the data with the right most number in a specific column and then fills to the left. So it is like:
Item one: 2.3 15.1 78.1
Item two: 1254.1 678.1 54283.1
I am using and fprintf statment something like:
fprintf(fid,"Item one: %d %d %d,A,B,C)
Using this method, it puts the numbers after a set of spaces and fills to the right, as if you were typing it. The behavour I want is to be able to get it to fill to the left so the rightmost column always lines up.
Is there a way to do that?
Thanks,

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 5월 23일
You need to include a field width in the format specifier.
An example for integers:
fprintf(fid, 'Item one: %8d %8d %8d', A, B, C);
An example for reals with 1 decimal place:
fprintf(fid, 'Item one: %8.1f %8.1f %8.1f', A, B, C);
Full details are here.
  댓글 수: 1
James Browne
James Browne 2021년 5월 25일
Great, thankyou!
I can see from that help you can also left justify the field width by using a '-' sign but still have the padding effect that I wanted.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by