How to write a value with zeros before the value in matlab?

조회 수: 2 (최근 30일)
Farshid Daryabor
Farshid Daryabor 2016년 11월 14일
댓글: Farshid Daryabor 2016년 11월 14일
Dear,
how to write/print a value with zeros before the value by the sprintf function, e.g., writing/printing the following form;
test_007120128O.nc
data_013587626O.nc
Regards,

채택된 답변

Adam
Adam 2016년 11월 14일
편집: Adam 2016년 11월 14일
sprintf( '%05.3d', 20 )
for example will pad leading 0s upto the number you specify (5 in this case, including the 20, so 3 additional 0s), but to get what you want depends what exactly you are starting with. If you always want 9 digits then
sprintf( '%09d', yourNumber )
should work
doc sprintf
covers this if you click on the 'formatSpec' hyperlink.
  댓글 수: 2
Adam
Adam 2016년 11월 14일
편집: Adam 2016년 11월 14일
In response to what you posted below, the syntax you posted doesn't seem to work at all, to even give what you show, but this works for me:
file=sprintf('%s%s_%09dO.nc', directory,'test',mynumber)
It is the rest of your sprintf format specification and input arguments that appear to not be quite right. For a start you specify 4 distinct fields, but only supply 3 inputs. The final %s that you have should not be there and mynumber should not be in a string.
Farshid Daryabor
Farshid Daryabor 2016년 11월 14일
Dear Adam,
Thank you very much, it's work perfect.
Best Regards

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

추가 답변 (1개)

Farshid Daryabor
Farshid Daryabor 2016년 11월 14일
Dear Adam,
Thank you for reply,
I tried the command you suggested me, but unfortunately it doesn't work.
>>directory = 'H:\home works\my folder\';
>>mynumber = 7120128;
>>file=sprintf('%s%s_%09d%sO.nc', directory,'test','mynumber')
>>file = H:\home works\my folder\test_000000055120128O.nc
file should be; file = H:\home works\my folder\test_007120128O.nc
Regards,
  댓글 수: 1
Adam
Adam 2016년 11월 14일
Please add as a comment to an answer rather than a new answer as it is less confusing.

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by